1. Wave four times

  1. The client process sends a connection release packet and stops sending data. Release the header of the data packet, FIN=1, whose sequence number is SEq = U (equal to the sequence number of the last byte of the previously transmitted data plus 1). At this point, the client enters the fin-WaIT-1 state. According to TCP, FIN packets consume a sequence number even if they do not carry data.

  2. After receiving the connection release packet, the server sends an acknowledgement packet with ACK=1, ACK= U +1 and its serial number seq= V. In this case, the server enters close-wait state. The TCP server notifies the higher-level application process that the client is released from the direction of the server. This state is half-closed, that is, the client has no data to send, but if the server sends data, the client still accepts it. This state also lasts for a period of time, i.e. the duration of the close-wait state.

  3. After receiving the acknowledgement request from the server, the client enters the fin-WaIT-2 state and waits for the server to send a connection release packet (before receiving the final data from the server).

  4. After sending the LAST data, the server sends a connection release packet with FIN=1 and ACK = U +1 to the client. The server is probably in the semi-closed state. Assume that the serial number is SEQ = W, then the server enters the last-ACK state and waits for the client’s confirmation.

  5. After receiving the connection release packet from the server, the client sends ACK=1, ACK= W +1 and its serial number is SEq = U +1. In this case, the client enters the time-wait state. Note that the TCP connection is not released at this time, and the client can enter the CLOSED state only after 2∗MSL (maximum packet segment life) and the corresponding TCB is revoked.

  6. The server enters the CLOSED state immediately after receiving an acknowledgement from the client. Similarly, revoking the TCB terminates the TCP connection. As you can see, the server ends the TCP connection earlier than the client.

1.1 Why Does a FIN Packet Segment Consume a Sequence number even if it does not carry data

For example, FIN packets do not consume a serial number. The client sends a 100-byte packet and a FIN packet and waits for confirmation from the server. If the client receives an ACK packet with an ACK value of 1000, it cannot know whether the packet is a 100-byte packet or a FIN packet.

1.2 Why wave four times

After receiving a FIN packet, the Server may not close the SOCKET immediately. Therefore, the Server can only reply an ACK packet to the Client, saying, “I received the FIN packet you sent.” I can send FIN packets only after all packets on the Server are sent. Therefore, THE FIN packets cannot be sent together. Therefore, a four-step handshake is required.

If the server determines that there is no data to send to the client, then of course it can merge the FIN and ACK into one package, making four waves three.

1.3 Simultaneous Shutdown

  1. When the application layer sends the shutdown command, both ends change from E S TA B L I S H E D to F I N _ WA I T _ 1.
  2. This will cause each party to send an F I N, and two F I N will be sent over the network to the other end.
  3. Upon receipt of F I N, the state changes from F I N _ WA I T _ 1 to C L O S I N G, and the final A C C K is sent. When the final A C K is received, the state changes to T I M E _ WA I T.