Why does TCP establish a connection with three handshakes, why not two or four?
- First handshake: The client sends a TCP packet, sets the SYN flag bit to 1, and saves the initial sequence number X in the serial number (Seq) of the packet header.
- Second handshake: the server responds to the acknowledgement packet, sets the SYN flag bit to 1, ACK bit to X+1, and saves the initial sequence number Y in the sequence number of the packet header.
- Third handshake: The client confirms the confirmation packet of the server with the SYN flag 0, ACK Y+1, and SEQUENCE number Z.
Why not twice?
** Look at the diagram again (S-server, C-client)**
- After the first handshake, S can confirm that the function of receiving and sending messages is normal, while C cannot confirm anything.
- After the second handshake, C can confirm that the sending and receiving functions of its own and S are normal, that is, the connection is established.
- For the third time, S can also confirm that the two parties can communicate normally.
Hypothetically, what if we got rid of the third time?
If it’s only the second time, the server and client are already set up, but what if the client doesn’t get a response from the server? At this time, the client thinks it is not established, but the server considers it successful and saves the necessary resources if a large number of such cases occur. The server will crash.
So a third handshake is necessary.
Why not four times
Because after three handshakes, C and S can at least confirm the previous communication, but cannot confirm the subsequent situation. So it doesn’t matter if you do it four times or five times or more.