Sockets 用python实现c#中的socket编程

Sockets 用python实现c#中的socket编程,sockets,process,buffer,Sockets,Process,Buffer,我试图通过在c#中运行外部程序(python脚本)并以线程形式打开服务器来运行udp通信 当数据传输到Python脚本打开的端口时,服务器(c#程序)接收数据。 但是,在发送一些数据后,不会捕获任何数据包 奇怪的是,如果我通过从外部打开cmd来直接运行Python脚本,它工作得很好 proInfo.RedirectStandardInput = true; proInfo.RedirectStandardOutput = true; 就在我让python进程在c#程序中运行时,观察到了这种奇怪

我试图通过在c#中运行外部程序(python脚本)并以线程形式打开服务器来运行udp通信

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

奇怪的是,如果我通过从外部打开cmd来直接运行Python脚本,它工作得很好

proInfo.RedirectStandardInput = true;
proInfo.RedirectStandardOutput = true;
就在我让python进程在c#程序中运行时,观察到了这种奇怪的现象

我怀疑发送缓冲区是原因,但我不知道如何修复它

如果你能帮助我,我会非常感激的

这是我的测试代码! c#udp服务器

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...")

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

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