1

I have a server/client app.

Both use BinaryReader/Writer when communicating.

When the client and server are exchanging messages rapidly, many in a given second, and I shutdown the server (via a built in command, orderly shutdown) most of the time (but not always) the client's BinaryReader.ReadString() method throws a EndOfStreamException, which is fine. The thing that I don't understand is why doesn't this exception change the TcpClient.Connected property to 'false'?

while(true){
   try{
      BinaryReader.ReadString()
   }
   catch(IOException){
     if(!TcpClient.Connected)
        break;
     //BinaryWriter.Write() - this will, eventually, change Connected property to 'false'
   }
}

This will loop endlessly. I thought that the Connected property changes on unsuccessful network read/write. If BinaryReader is throwing exceptions then it isn't successfully reading, is it?

If I throw in a BinaryWriter.Write(), then the endless loop is broken because Connected property is changed to 'false'.

Related question, does an EndOfStreamException always indicate a broken network connection or could it mean a temporary problem?

1 Answer 1

2

By design. From the Remarks section of the MSDN Library article for TcpClient.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

The workaround you discovered is the correct one.

Sign up to request clarification or add additional context in comments.

3 Comments

That I know, what I find puzzling is that a failed read does not change the Connected property. I can fail to read million times and Connected is still 'true'. According to MSDN a read OR write that is unsuccessful will change the Connected property, but in my example it seems untrue for reads, when they fail.
It appears as if BinaryRead() is reading the same data over and over again without hitting the socket. But then that would conflict with BinaryRead() MSDN doc, which seems to state, that unsuccessfully read data is never returned back to the stream.
After further testing, this appears to be the case: Failed BinaryRead will throw EndOfSocketException and does not change Connected property. Failed BinaryWrite will throw IOException->SocketException and will change Connected property. after which BinaryRead will throw IOException->SocketException with a similar message to the BinaryWrite exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.