Socket.EndRead 0字节意味着断开连接?

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)

Mar*_*off 6

来自文档:

If the remote host shuts down the Socket connection and all available data has been received, the EndRead method completes immediately and returns zero bytes.

So yes, zero bytes indicates a remote close.