TCP: connection-oriented, reliable transport-layer protocol. Its connection is based on a three-way handshake. Before talking about the three-way handshake, we should first understand its packet format, so that we can understand the three-way handshake more deeply.
TCP packet format
- The SYN flag bit in the header is used to establish the connection.
- The FIN flag bit on the front is used to disconnect the connection.
- The serial number and confirmation number are used to provide reliability services for TCP connections.
Three-way handshake
- First handshake: The CLIENT TCP sends a special TCP packet segment to the server TCP. The segment does not contain data at the application layer. The text will randomly generate a number and assign a value to
Serial number (client_isn)
. The SYN bit in the header is 1. Therefore, the packet is also called the SYN packet segment. - Second handshake: If the SYN segment reaches the server successfully, the server extracts the TCP SYN segment from the datagram and allocates TCP cache and variables to the connection. Finally, a connection segment is returned. The packet segment returned by the server contains three important bits: the SYN bit is set to 1;
Confirmation no.
The field value is client_ISN + 1; Randomly generate a number assigned toSerial number (server_isn)
. A modified packet segment is called a SYNACK packet segment. - Third handshake: Accepts the segment of the packet sent by the server and allocates cache and variables to the connection. Then the server sends an acknowledgement packet in which the SYN is 0.
Confirmation no.
The value of server_isn + 1.
Note: The packet segment of the third handshake can carry data from the client to the server, and the SYN value of the subsequent packet segment is 0.
After the three-way handshake is complete, the client and server can send the packet segment containing data.
In fact, the TCP three-way handshake is the same principle as any real communication. For example, when you are making a voice call to someone, you say, “Hello, can you hear me?” “, the recipient replies, “Yes, can you hear me?” . Again, you reply, “Yes.” At this point, you can communicate the content of the communication.
You are the client and they are the server.
Can you hear hello? – For the first of three TCP handshakes.
Can you hear me? Can you hear me? – Indicates the second TCP three-way handshake.
Your reply can be heard – corresponding to the third TCP handshake.
Four times to wave
- First wave: The client sends a special packet segment to the server. The FIN in the header of the packet segment is set to 1.
- Second wave: The server sends an ACK packet.
- Third wave: The server sends a packet whose FIN is 1.
- Fourth wave: The client sends an ACK packet.
Note: TCP connections wait some time after the fourth wave before disconnecting.
The problem
- Why three handshakes and not two?
A: To ensure reliable data transmission, both TCP communicators must maintain a serial number to identify which packets have been received.
The three-way handshake is a necessary step for communication parties to inform each other of the start sequence number and confirm that the other party has received the start sequence number. In the case of two handshakes, at most only the initial sequence number of the connection initiator can be confirmed, and the sequence number selected by the other party cannot be confirmed. The source of
Summary: Three handshakes are required to agree to determine the ISN (initial SEQ sequence number) of both parties.
The images in this article are from Computer Networking – the top-down Approach.
Reference links:
- Why is TCP three handshakes, not two or four?