What are CLOSE_WAIT and TIME_WAIT states?

172

50

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?

Lazer

Posted 2010-08-08T19:23:19.413

Reputation: 13 841

1Shameless plug to an answer on our sistersite [SF]. – Hennes – 2015-06-24T19:46:40.513

See http://webmasters.stackexchange.com/q/22946

– Dheeraj V.S. – 2013-06-07T09:15:14.253

see 'man netstat', scroll down to the state section: http://linux.die.net/man/8/netstat

– MaQleod – 2014-04-10T18:26:32.920

Answers

186

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.

BillThor

Posted 2010-08-08T19:23:19.413

Reputation: 9 384

But isnt this mean that, even if the packets arrive after function returned, they would be still discarded by the application? – MonsterMMORPG – 2017-03-20T15:58:03.657

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 – 2017-03-22T02:58:40.447

30

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

sleske

Posted 2010-08-08T19:23:19.413

Reputation: 19 887

1

@ChrisSmowton, So who is using the right terminology? The diagram or netstat? (cf.)

– Pacerier – 2016-01-23T03:38:17.723

@Pacerier I think they match -- where do you think they disagree? – Chris Smowton – 2016-02-01T16:42:53.890

@ChrisSmowton So this means next port owner may get extra bytes and that can break the response if we set TIME_WAIT = 0 ? – MonsterMMORPG – 2017-03-20T16:00:26.187

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 – 2017-03-20T21:12:59.980

17This 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 – 2014-04-10T12:11:31.013

1

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)

Denio Mariz

Posted 2010-08-08T19:23:19.413

Reputation: 129

1What does this add to the information provided by the existing answers? – fixer1234 – 2016-05-20T16:59:35.013

Adds a reference to RFC 793 – Denio Mariz – 2017-10-04T14:42:34.810