C#: How to determine if a TCPClient has been disconnected.

I’ve had a TCPClient disconnect but still have the connected property == true. So it seams the only way to tell is if a Read from the network stream returns 0 bytes. So right now I’m assuming it is disconnected if it returns 0 bytes. I was wondering if there is a better way?
[275 byte] By [BioSlayer] at [2007-12-23]
# 1

Hi BioSlayer

Here is documented REMARKS section for tcpClient.Connected property:

"The Connected property gets the connection state of the Client socket as of the last I/O

operation. When it returns false, the Client socket was either never connected, or is no

longer connected."

"Because the Connected property only reflects the state of the connection as of the most

recent operation, you should attempt to send or receive a message to determine the current

state. After the message send fails, this property no longer returns true. Note that this

behavior is by design. You cannot reliably test the state of the connection because, in the

time between the test and a send/receive, the connection could have been lost. Your code

should assume the socket is connected, and gracefully handle failed transmissions."

So, I thing yours 'Read' way is the best one and you may forget Connected property

alltogether!

Peca55 at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified