Jack Li's Blog

How TCP establish and close connection?

TCP establish connection #

ServerClientServerClientCLOSEDLISTENSYN-SENTSYN_RCVDESTABLISHEDESTABLISHEDSYN seq=100SYN ACK seq=300 ack=101ACK seq=101 ack=301

TCP close connection #

ServerClientServerClientESTABLISHEDESTABLISHEDFIN_WAITCLOSE_WAITLAST_ACKFIN_WAIT2TIME_WAITCLOSEDCLOSEDFIN seq=300ACK ack=301FIN seq=500 ack=301ACK seq=301 ack=501

Why TCP has TIME_WAIT? #

Client normally waits for 2 maximum segment lifetime (MSL) and close the connection. There are two main reason:

  • Ensure all old packets is disapper on the internet.
  • ACK from client might lose due to the network. Therefore, server will resend FIN after timeout. If client closes the connection immediately, client can’t receive the new FIN.