C#套接字在循环中接收时挂起-python作为套接字服务器

C#套接字在循环中接收时挂起-python作为套接字服务器,c#,python,.net,sockets,C#,Python,.net,Sockets,我熟悉C#,并且了解一些python。最近几天我在学习这本书,并运行了非常基本的套接字示例:和 它们在我的Windows python 3.x上运行良好 python服务器: from socket import * myHost = 'localhost' myPort = 50007 sockobj = socket(AF_INET, SOCK_STREAM) sockob

我熟悉C#,并且了解一些python。最近几天我在学习这本书,并运行了非常基本的套接字示例:和 它们在我的Windows python 3.x上运行良好

python服务器:

from socket import *
myHost = 'localhost'                           
myPort = 50007                          
sockobj = socket(AF_INET, SOCK_STREAM)       
sockobj.bind((myHost, myPort))
sockobj.listen(5)                            
while True:                                  
    connection, address = sockobj.accept()  
    print('Server connected by', address)   
    while True:
        data = connection.recv(1024)        
        if not data: break
        connection.send(b'Echo=>' + data)    
    connection.close()
现在我也想学习C#中的套接字,所以我编写了一个C#.net framework 4.5套接字客户端,希望接收并显示
echo client.py
的功能。 我从中获得了C#演示,并进行了一些重构以减少代码大小

        public static void Main(string[] args)
    {
        string server = "localhost";
        int port = 50007;
        string request = "GET / HTTP/1.1\r\nHost: " + server +
            "\r\nConnection: Close\r\n\r\n";
        Byte[] sent = Encoding.ASCII.GetBytes(request);
        Byte[] recv = new Byte[256];
        IPHostEntry hostEntry = Dns.GetHostEntry(server);
        IPEndPoint ipe = new IPEndPoint(hostEntry.AddressList[1], port);
        Socket s =
            new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        s.Connect(ipe);
        s.Send(sent, sent.Length, 0);
        int bytes = 0;
        string page = "recived:\r\n";
        //do
        {
            bytes = s.Receive(recv, recv.Length, 0);
            page = page + Encoding.ASCII.GetString(recv, 0, bytes);
        }
        //while (bytes > 0);
        Console.WriteLine(page);
        Console.WriteLine("result");
        Console.ReadKey();
    }
我的测试步骤:

  • 如果我使用本地IIS设置网站,例如 ,则上面的代码可以显示网页html 内容,这意味着我的C代码正在工作
  • 运行echo-server.py,将C#code的端口更改为50007,然后运行, 控制台中没有输出,并且应用程序不退出,如果我在循环中放置断点,我可以看到循环只运行了一次。python服务器确实输出了一些日志,表明C#正在连接
  • Comment do while循环(如代码中的注释),这次输出与echo client.py完全相同(预期)

  • 所以我想知道当我使用do-while循环时有什么问题

    s.Recieve()
    阻止执行,直到数据到达。但为什么当服务器在iis下时它运行良好?
    s.Recieve()
    阻止执行,直到数据到达。但为什么当服务器在iis下时它运行良好?