202

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?

4

3 Answers 3

226

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.

7
  • But isnt this mean that, even if the packets arrive after function returned, they would be still discarded by the application? Mar 20, 2017 at 15:58
  • 1
    @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, 2017 at 2:58
  • @BillThor: Are all these states included by ServerLimit or MaxRequestWorkers in Apache?
    – mahmood
    Jul 8, 2020 at 9:43
  • 1
    @mahmood Connections in the TIME_WAUT states are connections Apache has closed. Connections in CLOSE_WAIT state will be handled by Apache until it is read, which should be almost immediately after the connection was closed. These connections will not be included in ServerLibm or MaxRequestWorkers. To verify run the command "netstat -antp | grep WAIT".
    – BillThor
    Jul 16, 2020 at 15:39
  • @BillThor: Thanks. Honestly, I didn't expect an answer after all these years... I want to be sure that current connections are below the ServerLimit. Still I don't know which states have to be counted. Should I check only ESTABLISHED? Or others such as SYN_RECV?
    – mahmood
    Jul 17, 2020 at 10:20
35

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:

https://en.wikipedia.org/wiki/File:Tcp_state_diagram_fixed.svg

5
  • 20
    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. Apr 10, 2014 at 12:11
  • 1
    @ChrisSmowton, So who is using the right terminology? The diagram or netstat? (cf.)
    – Pacerier
    Jan 23, 2016 at 3:38
  • @Pacerier I think they match -- where do you think they disagree? Feb 1, 2016 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 ? Mar 20, 2017 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. Mar 20, 2017 at 21:12
2

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)

3
  • 1
    What does this add to the information provided by the existing answers?
    – fixer1234
    May 20, 2016 at 16:59
  • Adds a reference to RFC 793 Oct 4, 2017 at 14:42
  • Wikipedia wrong again. It is waiting for packets to expire.
    – user207421
    Apr 3 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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