Detecting Tcp Client Disconnect

c# detecting tcp disconnect

TcpClient / NetworkStream does not get notified when the connection is closed.
The only option available to you is to catch exceptions when writing to the stream.

A few years back we moved to using sockets instead of tcp client. socket is more usable as compared to tcpclient.

there are a couple of methods that you can use

Poll is one of them

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.poll.aspx

You can also do a check on outcome of Write itself. it gives you the number of bytes actually written.

The Connected property itself only reflects the state at the last operation. Its documentation states "The value of the Connected property reflects the state of the connection as of the most recent operation. If you need to determine the current state of the connection, make a non-blocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected."

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected.aspx

How can you detect when a TCP client disconnects with Python's select()?

When you use the keepalive, and it detects the failure, the select() should report the socket as both readable and writable. Then when you try to perform one of those operations you should get an error. Use try/except around the s.recv() call to detect socket.error.

You might naively assume that this would be reported as an "exceptional situation", but it isn't. That's used for normal exceptions on a valid socket, such as out-of-band data.

Is detecting a TCP socket disconnect in Python possible?

It is perfectly fine for TCP connections to not transfer any kind of data for a long time. And it is also not a problem if a cable gets disconnected as long as it is reconnected later data need to be transmitted.

The only way to be sure that the peer is still reachable is to have some kind of heartbeat. This can be either be done at the application level or it can be done at the TCP level - using TCP keep-alive. Usually systems offer a way to not only enable TCP keep-alive per socket but also to adjust how often a keep-alive packet will be send when the socket is idle, i.e. how quick an application can find out that the peer is no longer there. To get to the details on how to do this in Python see How to change tcp keepalive timer using python script?.

Instantly detect client disconnection from server socket

Since there are no events available to signal when the socket is disconnected, you will have to poll it at a frequency that is acceptable to you.

Using this extension method, you can have a reliable method to detect if a socket is disconnected.

static class SocketExtensions
{
public static bool IsConnected(this Socket socket)
{
try
{
return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
}
catch (SocketException) { return false; }
}
}

How to detect a TCP socket disconnection (with C Berkeley socket)

The only way you can detect that a socket is connected is by writing to it.

Getting a error on read()/recv() will indicate that the connection is broken, but not getting an error when reading doesn't mean that the connection is up.

You may be interested in reading this:
http://lkml.indiana.edu/hypermail/linux/kernel/0106.1/1154.html

In addition, using TCP Keep Alive may help distinguish between inactive and broken connections (by sending something at regular intervals even if there's no data to be sent by the application).

(EDIT: Removed incorrect sentence as pointed out by @Damon, thanks.)



Related Topics



Leave a reply



Submit