Red*_*dth 4 c# sockets asynchronous
我想知道在c#中的异步套接字中,在EndRead调用中接收0个字节意味着服务器实际上已经断开了我们的连接吗?
我看到的很多例子都表明情况就是这样,但我收到的断线频率要高得多.
这段代码是否正确?或者endResult <= 0对连接状态没有任何意义?
private void socket_EndRead(IAsyncResult asyncResult)
{
//Get the socket from the result state
Socket socket = asyncResult.AsyncState as Socket;
//End the read
int endResult = Socket.EndRead(asyncResult);
if (endResult > 0)
{
//Do something with the data here
}
else
{
//Server closed connection?
}
}
Run Code Online (Sandbox Code Playgroud)