Three-way Handshake, Four-way Handshake

Host A sends a connection request to Host B; Host B acknowledges the segment received from Host A; Host A re-acknowledges the confirmation from Host B.

TCP is the transport layer protocol of the Internet, which uses the three-way handshake protocol to establish a connection. After the client actively issues a SYN connection request, it waits for the other party to send a SYN+ACK, and finally performs an ACK confirmation on the other party's SYN. This method of establishing a connection prevents the generation of erroneous connections. The flow control protocol used by TCP is a variable-sized sliding window protocol.

  1. First Handshake: The client sets the flag bit SYN to 1, randomly generates a sequence number seq=x, and sends this data packet to the server. The client enters the syn_sent state and waits for confirmation from the server.
  2. Second Handshake: Upon receiving the data packet, the server recognizes the client's request to establish a connection via the flag bit SYN=1. The server sets both the SYN and ACK flag bits to 1, sets ack=x+1, randomly generates a sequence number seq=y, and sends this data packet back to the client to confirm the connection request. The server enters the syn_rcvd state.
  3. Third Handshake: Upon receiving the confirmation, the client checks it. If correct, it sets the ACK flag bit to 1, sets ack=y+1, and sends this data packet to the server. The server performs a check; if correct, the connection is successfully established. Both the client and the server enter the established state, completing the three-way handshake. Subsequently, data transmission can begin between the client and the server.

Four-way Wave

  1. First Wave: The client sends a FIN to close the data transmission from the client to the server. The client enters the fin_wait_1 state.
  2. Second Wave: Upon receiving the FIN, the server sends an ACK to the client, with the confirmation sequence number being the received sequence number + 1. The server enters the close_wait state. At this point, the TCP connection is in a half-closed state; that is, the client has no more data to send, but if the server sends data, the client must still receive it.
  3. Third Wave: The server sends a FIN to close the data transmission from the server to the client. The server enters the last_ack state.
  4. Fourth Wave: Upon receiving the FIN, the client enters the time_wait state and then sends an ACK to the server. After confirmation, the server enters the closed state, completing the four-way wave.