Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've started using sockets in programming with Java (I have experience with Python), and while testing a server that I'd written with the telnet command, I noticed that the utility responds immediately to the server being killed. When I write a client with Java (or Python), it takes at the very least a few seconds for the program indicate that it has been disconnected. As far as I know, there isn't a way to test if a socket is connected, short of a heartbeat message, which my server was not written to accept or receive.

My question is, how to the folks at GNU know when my socket has been disconnected? Is there a part of TCP that I failed to find in my searching that would allow this? Thanks.

share|improve this question

There are two ways to close a TCP session, the correct way for an established session is a three way tear down, which involves:

 Closing party -> Other end: FIN
 Other end -> Closing party: FIN ACK
 Closing party -> Other end: ACK

So this sequence lets both ends know that the other end understands the session to be closed.

The other method is an RST or reset packet, which is normally sent in the event of an issue. It is sent immediately when the receiving party is not listening on the TCP port being connected to, and can also be sent in the event of an error in the application.

In your case, the FIN ACK sequence would be how the connection should be terminated, and this should be instigated by whatever library you are using when you close the socket.

If I were you I would get wireshark or similar set up and see exactly what happens during a tear down.

share|improve this answer
Btw, if the session is not closed properly and the other end "goes away", then it will be cleared from the session table at some future time - this varies from OS to OS. – Paul Nov 6 '11 at 1:55
I don't see how that accounts for the difference in the time it takes for the telnet utility to notice it vs my utility. Is it a difference in the socket libraries that they are using? – Michael Smith Nov 6 '11 at 15:53
It is impossible to account for the differences without knowing more about what your utility does in response to the traffic it sees - which is why I suggested wireshark. If you see the fin-ack sequence come in, and yet your utility still has the socket defined as open, it is either a bug in the underlying library or the utility itself. – Paul Nov 6 '11 at 20:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.