What is a three-way handshake and what is a four-way wave?

TCP: The TCP Transport Control Protocol (TCP) is a connection-oriented, reliable, byte stream based Transport layer communication Protocol. It is designed to provide reliable end-to-end byte stream over unreliable Internet networks. It is a transport-layer protocol between application layer and network layer. TCP, which divides data packets into segments and sends them to the IP protocol to the receiving end.

Three-way Handshake: Three packets are sent between the client and server to establish a TCP connection.

Process:

  • First handshake: The client sends a SYN packet, which indicates that the ISN(C) sequence number is initialized by the client, and then enters the SYN_SENT state.

  • Second handshake: After receiving the packet from the client, the server responds with its SYN packet and uses the ISN+1 as the ACK value. In this case, the server is in the SYN_RCVD state.

  • Third handshake: After receiving the SYN packet, the client sends an ACK packet with the ISN+1 of the server, and the client is in the ESTABLISHED state. After receiving the ACK packet, the server is also in the ESTABLISHED state, which establishes a connection.

Why not 2 handshakes?

How does the server know if the data was sent successfully because the server sent it to the client the second time? Only the server passed but did not answer, do not know whether it was successful, so it will be the third handshake.

In addition, the client may send multiple request packets due to network congestion, and the delayed request packets are connected to the server, wasting resources of the server

Four waves: TCP terminates a connection with four waves

Process:

  • First wave: The client sends a FIN packet with a specified sequence number. At this point, the client is in FIN_WAIT1 state and stops sending data waiting for confirmation from the server

  • Second wave: After receiving the FIN, the server sends an ACK packet and uses the serial number of the client +1 as the SEQUENCE number of the ACK packet, indicating that the packet has been received. In this case, the server is in CLOSE_WAIT state

  • Third wave: If the server also wants to disconnect, the server sends a FIN packet with a specified sequence number as the first wave from the client. The server is in the LAST_ACK state

  • Fourth wave: After receiving the FIN, the client also sends an ACK packet and uses the serial number of the server +1 as the sequence number of its OWN ACK packet. In this case, the client is in TIME_WAIT state. It takes a period of time to ensure that the server receives its ACK packet before it enters the CLOSED state. After receiving the ACK packet, the server is in the CLOSED state

Why do you need four waves?

After receiving a Fin packet from the client, the server does not immediately close the connection. Instead, it sends an ACK packet to inform the client that the request for closing the connection is received. The server sends a Fin packet to disconnect the connection only after all the packets are sent

Three handshakes and four waves

Refer to the link

Wechat official account: JS daily question