HTTP Three handshakes and four waves

HTTP overview

HTTP is short for Hypertext Transfer Protocol (HYPERtext Transfer Protocol). It is an application-layer protocol of TCP/IP and is used to define the process of exchanging data between WEB browsers and WEB servers. To obtain a Web resource from the Web server after the client is connected to the Web server, the client must comply with a certain communication format. HTTP defines the communication format between the client and the Web server

About TCP

  • TCP provides a connection-oriented, reliable byte stream service
  • In a TCP connection, only two parties communicate with each other. Broadcast and multicast cannot be used with TCP
  • TCP uses checksum, acknowledgement, and retransmission mechanisms to ensure reliable transmission
  • TCP sorts data into sections and uses cumulative validation to ensure that the order of the data is constant and non-repetitive
  • TCP uses the sliding window mechanism to control traffic, and dynamically changes the window size to control congestion

TCP does not guarantee that the data will be received by the other party, because it is impossible. It is not a 100% reliable protocol; all it can provide is reliable delivery of data or reliable notification of failures.

Three-way handshake

Three-way Handshake Sets up a TCP connection with Three packets sent by the client and server.

  1. First handshake ([SYN], Seq = x) The client sends a SYN packet with the initial sequence number x. After the packet is sent, the client enters the SYN_SEND state.

  2. Second handshake ([SYN,ACK], Seq = y, ACK = x + 1) The server replies with an ACK and also sends a SYN packet. ACK = x + 1: confirms receipt (Seq value + 1 from the client). Seq = y: asks the client to confirm receipt. After sending the packet, the server enters the SYN_RCVD state.

  3. Handshake for the third time ([ACK], ACK = y + 1) The client sends an ACK packet again. ACK = y + 1 indicates that the client acknowledges receiving the packet from the server (Seq value from the server + 1). After sending the packet, the client enters the ESTABLISHED state. The server receives the packet and enters the ESTABLISHED state. The TCP handshake is complete.

Why three handshakes? Not twice or four times?

  • Hypothetically speaking, what would happen if there were two handshakes? After the server sends the reply message, it cannot confirm that the client received the message, which means that only the client can send data to the server.
  • How about four handshakes? Why waste performance sending a message when you already have a stable transmission stream?
  • Therefore, three times is the most appropriate. Here I simply analyze it from the perspective of individuals, not from the perspective of sequence and other principles.

Four times to wave

The disconnection of a TCP connection requires four packets to be sent, so it is called the quad wave.

  1. First wave ([FIN], Seq = u) The client sends a fin-flagged packet telling the server that it needs to close the connection, indicating that it is no longer sending data, but can still receive it. After the sending is complete, the client enters the FIN_WAIT_1 state.

  2. Second wave ([ACK], ACK = u + 1, Seq = v) The server sends an ACK acknowledgement, telling the client that it received the closed request but is not ready. When the packet is sent, the server goes into CLOSE_WAIT state. When the client receives the packet, it goes into FIN_WAIT_2 and waits for the server to close the connection.

  3. Third wave ([FIN], Seq = W, ACK = U + 1) When the server is ready to close the connection, it sends a fin-flagged packet telling the client that it is ready to close. After the sending is complete, the server enters the LAST_ACK state and waits for confirmation from the client.

  4. Fourth wave ([ACK], ACK = w + 1) After receiving the shutdown request from the server, the client sends an ACK packet and enters the TIME_WAIT state, waiting for the ACK packet that the server may request to retransmit. After receiving the ACK packet, the server closes the connection and enters the CLOSED state. If the client does not receive the ACK packet from the service after waiting for a fixed period of time (two maximum life cycles), the client considers that the server has CLOSED the connection and then closes the connection itself and enters the CLOSED state.

Why three handshakes, but four waves? Can’t you wave three times?

Assuming three waves, the server sends the SYN and ACK packets after receiving the shutdown request from the client. In this case, the server still has data to send, resulting in data loss. Therefore, it is necessary to wait for the server to send the rest of the data.


HTTP simple workflow