Detect Socket Disconnected
The Connected property shows the state of the socket at the timeof the last IO operation. Thus the socket could be closed by theremote host with the Connected property value still being true.
Thevalue won't change until after you try reading or writing to thesocket.You should be able to use the Available property to determine ifthe remote host has closed the connection.
The Available property willeither return the number of bytes available to be read on the socket(remember that its possible for a connected socket to still returnzero bytes of data), or will throw a SocketException if the remotehost shuts down or has closed the connection.
All you should need todo is catch the SocketException in your code. (note that this behaviorchanged in .NET 1.1. In 1.0 the Available property just returned zeroif the remote host closed the connection).Alternatively, you can also utilize the Poll() Socket method withthe SelectRead SelectMode parameter.
This method will return a truevalue if data is available or if the connection has been closed by theremote host. You will then need to differentiate between which ofthese situations has occurred (by reading the socket buffer, andseeing if it returns a zero value).Hope this helps shed some light on your problem.Rich Blum - Author"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176"Network Performance Open Source Toolkit" (Wiley)http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html
