Chad Schouggins
Chad Schouggins

Reputation: 3521

.NET TCPClient Connection State

Background

    I am communicating with a server (that I can't touch, of course) over a TCP connection. The application I am connecting to requires two connections. One as an "I want to connect", handshake type thing and a second, on a different port, for the actual connection. After days of banging my head against this thing, I found out that for the second connection to be accepted, the first disconnect (FIN) must be initiated by the server (which it does immediately). If the client initiates the disconnect, the second connection will be refused.

Question:

    I have read many posts about this, and I know about TCP connections and how you can't really know if the conneciton active and all that (please don't respond with dropped connections, broken lines, unexpected shutdowns, etc). But is there any way to detect that the remote server has disconnected properly without attempting to write data to it.

    Again, I'm not talking about dropped connections, broken lines, whatever. I want to know when the server sends a FIN segment that I have ACKed. Sounds simple enough, right?

Upvotes: 1

Views: 445

Answers (1)

Kristian Evensen
Kristian Evensen

Reputation: 1345

If I have understood the NetworkStream-documentation properly. When you do a read from the NetworkStream (either synchronous or async), and it returns 0 bytes, it means that the remote party has closed down the connection. I.e., the FIN has been ACK'ed.

Upvotes: 2

Related Questions