C#套接字在循环中接收时挂起-python作为套接字服务器
我熟悉C#,并且了解一些python。最近几天我在学习这本书,并运行了非常基本的套接字示例:和 它们在我的Windows python 3.x上运行良好 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
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();
}
我的测试步骤:
所以我想知道当我使用do-while循环时有什么问题
s.Recieve()
阻止执行,直到数据到达。但为什么当服务器在iis下时它运行良好?s.Recieve()
阻止执行,直到数据到达。但为什么当服务器在iis下时它运行良好?