When I do netstat -a on my Windows machine, I get a listing of the ports with one of the four states:

- LISTENING
- CLOSE_WAIT
- TIME_WAIT
- ESTABLISHED

What do CLOSE_WAIT and TIME_WAIT mean/indicate?

up vote 164 down vote accepted

Due to the way TCP/IP works, connections can not be closed immediately. Packets may arrive out of order or be retransmitted after the connection has been closed. CLOSE_WAIT indicates that the remote endpoint (other side of the connection) has closed the connection. TIME_WAIT indicates that local endpoint (this side) has closed the connection. The connection is being kept around so that any delayed packets can be matched to the connection and handled appropriately. The connections will be removed when they time out within four minutes. See http://en.wikipedia.org/wiki/Transmission_Control_Protocol for more details.

  • But isnt this mean that, even if the packets arrive after function returned, they would be still discarded by the application? – MonsterMMORPG Mar 20 '17 at 15:58
  • @MonsterMMORPG Packets that arrive out of order after the connection has been closed will be handled by the network stack. These can be usually be safely discarded according to normal duplicate packet rules. Packets that appear to be related to an unknown active connection are normally discarded, and generate a response. The WAIT states protect against this traffic. – BillThor Mar 22 '17 at 2:58

Basically the "WAIT" states mean that one side closed the connection but the final confirmation of the close is pending.

See e.g. this diagram of TCP states for details:

http://www.jxos.org/Projects/TCP/tcpstate.html

  • 11
    This accurately describes CLOSE_WAIT but not TIME_WAIT. TIME_WAIT indicates that the local application closed the connection, and the other side acknowledged and sent a FIN of its own. We're now waiting for any stray duplicate packets that may upset a new user of the same port. – Chris Smowton Apr 10 '14 at 12:11
  • 1
    @ChrisSmowton, So who is using the right terminology? The diagram or netstat? (cf.) – Pacerier Jan 23 '16 at 3:38
  • @Pacerier I think they match -- where do you think they disagree? – Chris Smowton Feb 1 '16 at 16:42
  • @ChrisSmowton So this means next port owner may get extra bytes and that can break the response if we set TIME_WAIT = 0 ? – MonsterMMORPG Mar 20 '17 at 16:00
  • Possible but very unlikely, as the sequence numbers would need to match for the application to see the rogue packet spliced into the stream, or the receiver would need to buffer the apparently out of order packet until the right sequence number came around. I don't know enough about practical implementations to tell you whether the latter is done in practice. – Chris Smowton Mar 20 '17 at 21:12

TIME_WAIT represents waiting for enough time to be sure that remote TCP received the ACK of its FIN request. See en.wikipedia.org/wiki/Transmission_Control_Protocol (and also RFC 793)

  • 1
    What does this add to the information provided by the existing answers? – fixer1234 May 20 '16 at 16:59
  • Adds a reference to RFC 793 – Denio Mariz Oct 4 '17 at 14:42

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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