Can Socket.Receive () return 0 if the connection is still open?

I am writing a .NET C # application that sends and receives data through Sockets, and I am having some problems if the client application crashes without closing the socket in the correct way.

I set the "receive time" to the given value, and I expected Socket.Receive () to throw an exception after this period of time. But instead, the method simply returns 0.

So my question is: is it possible that the socket is still open if Socket.Receive() returns 0? Or can I safely assume that it does not work?

(Maybe a little hard to understand. If so, let me know in the comments)

+4
source share
2 answers

Nope. If you get 0.

From MSDN:

If the remote host disconnects the Socket connection with the Shutdown method and all available data has been received, the Receive method will end immediately and return zero bytes.

http://msdn.microsoft.com/en-us/library/8s4y8aff.aspx

+12
source

Your question is a bit confusing. The socket is open until you close it. The connection is open until both ends close it. The read side of the connection can be closed by the sender closing it for output. Therefore, when reading the connection, you will get zero if the peer either closed the socket or turned it off for output. At this point, your socket is still open, although you must close it.

It will also return zero if the local application disconnects the input socket.

+1
source

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


All Articles