I have a simple program that uses TcpClient and SslStream to write data to a socket.
To test it I ran the program over night so my program would open the connection, write nothing for a long time, so the firewall or remote server would close the connection. This morning I took a look at TCPView and verified the connection was closed and then told my program to Write to the socket.
No exception was thrown on Write. However, the next Write did thrown this exception as expected: “System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host”
I can see why TcpClient.Connected could return True even if it really wasn’t connected, but why would Write not throw an exception on a connection that was actually closed (as verified in TCPView)?
That's because of the way TCP/IP protocol works. When you call TcpClient.Write data is being sent to the server, and the function returns success without waiting for a server response.
In the meantime the server returns an error. Tcp/IP stack on your side notices that and next time you try to write it will throw an exception
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With