Socket.EndRead 0 bytes means disabled?

I am wondering if in my Async Sockets in C #, getting 0 bytes in an EndRead call, does the server really disconnect us?

Many of the examples that I see show that this is so, but I get disconnects much more often than I expected.

Is this code correct? Or does endResult <= 0 mean nothing about the state of the connection?

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?  
  }
}
+3
source share
2 answers

0 read length should mean a graceful shutdown. Disable throws error (10054, 10053 or 10051).

, , 0, , - 0 . : . , , . , , , , 995 ERROR_OPERATION_ABORTED, . , , , (.. ) 995, 0.

+1

:

Socket , EndRead .

, , .

+5

Source: https://habr.com/ru/post/1717168/


All Articles