0
Reply

TcpClient.Connected=true with tcpclient and stream closed

Carlo Arnaboldi

Carlo Arnaboldi

Aug 28 2013 2:43 PM
1.2k
Let's say you have 2 applications, server and client. I'm debugging them at the same time in 2 instances of visual studio 2010. Let's say that on the client side I call:


_reader.Close(); //BinaryReader

_writer.Close(); //BinaryWriter

_ssl.Close();    //SslStream taken from _stream

_stream.Close(); //NetworkStream from _client.GetStream();

_client.Close(); //TcpClient



and I've checked through the debugger that this code is executed. After this, I also trigger, with other instructions, the closure of the client application, infact visual studio exits from debug mode and goes back to standard mode.


In the server application, since the connection is actually closed, a pending _reader.Read() fails, triggering an exception which is properly handled in a try/catch. This, according to TcpClient.Connected Remarks should update the value of TcpClient.Connected accordingly.


This doesn't happen: a different thread in the server keeps checking _client.Connected but it's still true.


You should note that:


1) If I close the client using the "Stop" button everything works seamlessly, _client.Connected becomes false


2) Don't know is this may be relevant, but server listens on 127.0.0.1 and client connects to 127.0.0.1 since I'm debugging these 2 apps on a single pc