插座:如何保持打开状态?

2024-09-27 07:29:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个使用套接字进行通信的简单服务器(C#)和客户机(python)。在

服务器创建一个

var listener = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp)

然后绑定,在无限循环中监听

^{pr2}$

AcceptCallback调用BeginReceive,其callbackk读取缓冲区并向客户端发回消息

一切正常。客户机发送消息,服务器看到,回复,客户机看到。但当客户机再次尝试发送消息时(因为测试客户机处于循环中),什么都不会发生

python客户端:

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
while True:
    sock.sendall(data)
    blockLen = sock.recv(32) #server sends back a string prefixed with how long the string is
    serverReply = sock.recv(blockLen)

我如何防止插座死掉?在


Tags: 服务器消息客户端new客户机stringvarsocket
1条回答
网友
1楼 · 发布于 2024-09-27 07:29:56

来自MSDN(Remarks on EndAccept):

Your callback method should invoke the EndAccept method. When your application calls BeginAccept, the system usually uses a separate thread to execute the specified callback method and blocks on EndAccept until a pending connection is retrieved. EndAccept will return a new Socket object that you can use to send and receive data with the remote host. You cannot use this returned Socket to accept any additional connections from the connection queue. If you want the original thread to block after you call the BeginAccept method, use WaitHandle.WaitOne. Call the Set method on a ManualResetEvent in the callback method when you want the original thread to continue executing.

还有一个关于stackoverflow check的问题,有帮助吗?在

相关问题 更多 >

    热门问题