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.
- 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_sentstate and waits for confirmation from the server. - 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_rcvdstate. - 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
establishedstate, completing the three-way handshake. Subsequently, data transmission can begin between the client and the server.
Four-way Wave
- First Wave: The client sends a FIN to close the data transmission from the client to the server. The client enters the
fin_wait_1state. - 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_waitstate. 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. - Third Wave: The server sends a FIN to close the data transmission from the server to the client. The server enters the
last_ackstate. - Fourth Wave: Upon receiving the FIN, the client enters the
time_waitstate and then sends an ACK to the server. After confirmation, the server enters theclosedstate, completing the four-way wave.