【问题标题】:socket programming in c# with python在 c# 中使用 python 进行套接字编程
【发布时间】:2019-11-20 06:55:26
【问题描述】:

我正在尝试通过在 c# 中运行外部程序(python 脚本)并将服务器作为线程打开来运行 udp 通信。

当数据传输到Python脚本打开的端口时,服务器(c#程序)接收数据。 但是,在发送了一些数据之后,并没有捕获到任何数据包。

奇怪的是,如果我通过从外部打开 cmd 直接运行 Python 脚本,它运行良好!

这个奇怪的现象是在我让python进程在c#程序中运行的时候出现的!

我怀疑发送缓冲区是原因,但我不知道如何解决它。

如果你能帮助我,我将不胜感激!

这是我的测试代码! 时间:2019-05-10 标签:c#udp server

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;

namespace UdpSocket
{
    public partial class Form1 : Form
    {
        bool used = true;
        Thread t1 = null;
        Process current_pro = null;
        UdpClient srv = null;

        public Form1()
        {
            InitializeComponent();

            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.GridLines = true;
            listView1.Columns.Add("Timeline", 800, HorizontalAlignment.Center);
        }

        private void udpserverStart()
        {
            try
            {
                srv = new UdpClient(5582);
                IPEndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);

                while (used)
                {
                    byte[] dgram = srv.Receive(ref clientEP);
                    listView1.Items.Add(Encoding.Default.GetString(dgram));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {

            }
        }

        private void hookStart()
        {
            ProcessStartInfo proInfo = new ProcessStartInfo();
            proInfo.FileName = "python.exe";
            proInfo.Arguments = String.Format("-u {0}", @"output.py");
            proInfo.CreateNoWindow = true;
            proInfo.UseShellExecute = false;
            proInfo.RedirectStandardInput = true;
            proInfo.RedirectStandardOutput = true;

            current_pro = new Process();
            current_pro.StartInfo = proInfo;
            current_pro.Start();
            current_pro.Exited += (sender, e) =>
            {
                MessageBox.Show("Hook Process exited!");
            };

        }

        private void button1_Click(object sender, EventArgs e)
        {
            t1 = new Thread(udpserverStart);
            t1.Start();
            hookStart();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            used = false;
            srv.Close();
            //t1.Join();
            t1.Abort();
            current_pro.Kill();
            this.Close();
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

和python udp客户端

sc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sc.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 100000)

def message(message, data):
    if message['type'] == 'send':
        try:
            payload = str(message['payload']) + '\n'
            sc.sendto(payload.encode(), ('127.0.0.1', 5582))
            print(str(message['payload']) + '\n')
        except:
            print('error');
    elif message['type'] == 'error':
        try:
            print(str(message['stack']) + '\n')
        except:
            print('error');
    else:
        print("something...")

【问题讨论】:

    标签: sockets process buffer


    【解决方案1】:

    我只是尝试删除下面的重定向代码,效果很好!

    proInfo.RedirectStandardInput = true;
    proInfo.RedirectStandardOutput = true;
    

    【讨论】:

      猜你喜欢
      • 2019-05-03
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多