Did you check if TCP keep alive
is set? That should detect if connection is broken.
Also, check if you got zero length bytes on receive, this means that connection is closed.
Edit: Keep Alive is standard method to check if connection is active, this means that one side sends small chunks of data to the other side periodically: http://en.wikipedia.org/wiki/Keepalive.
Many components have this feature already implemented. I've never used TcpClient
class, but it must be some wrapper class for Socket
over TCP and, as doc states, this class have exposed underlaying Socket
(you can use Socket
class for TCP
also, but never mind). And this Socket
have method SetSocketOption
. So try:
client.Socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, true);