Closed network connection detection

I have written several small programs that communicate via TCP. I have endless problems with the system freezing because one program closed its network connection, and the other endpoint for some reason did not notice that it was now disconnected.

I was expecting I / O on a TCP connection that was closed to throw some kind of I / O exception, but instead, the program seems to just freeze, waiting for the other endpoint to respond. Obviously, if the connection is closed, this answer never comes. (It doesn't even seem to time out if you leave it, say, twenty minutes.)

Is there a way to make the remote end “see” that I have closed the network connection?

Update: Here is the code ...

public sealed class Client { public void Connect(IPAddress target) { var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(ipAddress, 1177); _stream = new NetworkStream(socket); } public void Disconnect() { _stream.Close(); } } public sealed class Server { public void Listen() { var listener = new TcpListener(IPAddress.Any, 1177); listener.Start(); var socket = listener.AcceptSocket(); _stream = new NetworkStream(socket); ... } public void Disconnect() { socket.Shutdown(SocketShutdown.Both); socket.Disconnect(false); } } 
+4
source share
3 answers

When the application closes the socket in the correct way, it sends a message containing 0 bytes. In some cases, you may get a SocketException , showing that something went wrong. In the third situation, the remote side is no longer connected (for example, by disconnecting the network cable) without any connection between the two sides.

If this happens, you will have to write data to the socket to find that you can no longer contact the remote side. This is why life support mechanisms were invented - they check each so often whether they can still communicate with the other side.

Look at the code you sent now: when using NetworkStream Read operation on it will return 0 (bytes) to indicate that the client has closed the connection.

The documentation is referred to as

"If there is no data to read, the Read method returns 0.

and

"If the remote host disconnects the connection and all available data has been received, the Read method exits immediately and returns zero bytes."

in the same paragraph. In fact, NetworkStream blocks if data is not readable while the connection is open.

+2
source

Hi MathematicalOrchid,

You can find what you are looking for.

http://blog.stephencleary.com/2009/05/detection-of-half-open-dropped.html

There is some excellent information there when it comes to working with TCP sockets and detects half-open connections.

You can also refer to this post, which seems to have the same solution:

TcpClient communication with server to maintain connection in C #?

-Dave

+1
source

You open a socket and assign it to a stream. At the end of the process, you close the network stream, but not the socket.

For NetworkStream.Close() , to close the underlying socket, it must have the property parameters set to true in the constructor. See MSDN docs at http://msdn.microsoft.com/en-us/library/te7e60bx.aspx .

This can cause the connection to hang because the main socket was not properly closed.

Edit

 _stream = new NetworkStream(socket); 

For

 _stream = new NetworkStream(socket, true); 

On the other hand, if you do not require maximum performance for your small application, try using TCPClient - http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient%28v=vs.100 % 29.aspx . This is a wrapper around the socket and provides a means of checking the status of the connection.

+1
source

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


All Articles