TCP is a network protocol that we come into contact with almost every day. Most network connections are established based on TCP. Establishing a connection using TCP is a three-way handshake, as anyone who has studied computer networking or knows a little about TCP knows.

If we talk briefly about the process of establishing a TCP connection, I’m sure many people who have prepared for an interview will know this very well, but if you want to delve into “why does TCP require three handshakes to establish a connection?” Why on earth do we need three handshakes to establish a TCP connection?

An overview of the

First of all, we all know that shaking hands is to establish a connection, so why do you have to shake hands three times?

Client ------SYN-----> Server
Client <---ACK/SYN---- Server
Client ------ACK-----> Server
Copy the code

Why not just this one?

Client ------SYN-----> Server
Client <-----ACK------ Server
Copy the code

So we need to break down the handshake into what it’s really doing.

TCP is a two-way communication protocol, which means that either end should be able to reliably send data. Both parties need to establish a serial number, and both parties need to confirm the serial number of the other party.

To establish a TCP connection with the server, the client needs to confirm these four things through a handshake

1. The server needs to confirm that it can receive data packets from the client.

2. The client needs to confirm that it can receive data packets from the server.

3. The client needs to confirm one thing: The server can receive data packets from the client.

4. The server needs to confirm one thing: The client can receive packets from the server.

Let’s go back to “So why do we have to shake hands three times? How about two?” That’s the question.

Client ——SYN—–> Server, thing 1 is confirmed.

Client <– ACK/SYN—- Server, things 2 and 3 are confirmed.

Therefore, a third handshake is required to confirm thing 4, i.e

Client ——ACK—–> Server

So far, we’ve seen why three handshakes are required to establish a TCP connection.

But what if we look at this in terms of what a TCP connection is? Let’s start with

The reliability and flow control mechanisms described above require that TCPs initialize and maintain certain status information for each data stream. The combination of this information,including sockets, sequence numbers, and window sizes, is called a connection.

The documentation clearly defines what a connection is in TCP, so let’s summarize: the information used to ensure reliability and flow control mechanisms, including sockets, serial numbers, and window sizes, is called a connection.

Therefore, to establish a TCP connection, both sides of communication need to reach a consensus on the above three kinds of information. A pair of sockets in the connection is composed of the Internet address identifier and port, the window size is mainly used for flow control, and the serial number is used to track the serial number of packets sent by the communication initiator. The receiver can confirm the successful reception of a packet to the sender through the serial number.

At this point, we’ve converted the original question to “Why do I need three handshakes to initialize Sockets, window sizes, and initial serial numbers? “, so let’s start to analyze this detailed problem and look for an explanation.

design

So let’s focus on the following aspects to explain “Why do I need three handshakes to initialize Sockets, window sizes, and initial serial numbers? “

1. A three-way handshake is required to prevent the initialization of a repeated historical connection.

2. Initialize the initial serial number of the two communication parties by shaking hands three times.

3. Discuss the possibility of establishing a connection with other handshakes.

Prevents initialization of repeated history connections

The principle reason for the three-way handshake is to prevent old duplicate connection initiations from causing confusion.

The document points out the primary reason for using the three-way handshake for TCP connections — to prevent messy problems caused by historical re-connection initializations, and to prevent two parties communicating over TCP from establishing the wrong connection.

In the case of poor or complex network conditions, if an already sent connection request is not acknowledged within the timeout period, the sender sends several consecutive connection establishment requests. Imagine if TCP established a connection with only two handshakes, the two parties would establish a false connection because the receiver would receive the connection request without knowing that it was a connection that had expired due to network congestion.

Therefore, TCP chooses to use the three-way handshake to establish the connection and introduces the control message RST in the connection. When receiving the request, the receiver will send the SEQ+1 sent by the sender to the other party. At this time, the sender determines whether the current connection is historical:

1. If the current connection is historical, that is, SEQ expires or times out, the sender directly sends an RST control message to terminate the current connection.

2. If the current connection is not a historical connection, the sender sends an ACK control message, and the communication parties successfully establish the connection.

Using the three-way handshake and RST control messages gives the ultimate control over whether or not a connection is established to the sender, because only the sender has enough context to determine whether the current connection is faulty or expired, which is the main reason for using the three-way handshake to establish a TCP connection.

Initializes the initial serial numbers of the communication parties

Another using a three-way handshake communication is one of the important reason why both sides need to obtain an initialization sequence number used to send information, as a reliable transport layer protocol, TCP needs in the unstable network environment to build a reliable transport layer, the uncertainty of the network may lead to packet loss and order problems, common problems might include:

1. Data packets are repeatedly sent by the sender, causing data duplication.

2. Packets are lost by routes or other nodes during transmission.

3. Packets may not be sent in the correct order when they arrive at the receiver.

To solve these possible problems, TCP requires the sender to add a “sequence number” field to the packet. With the sequence number of the packet, we can:

1. The recipient can de-duplicate duplicate data packets by serial number.

2. The sender sends the packet repeatedly if the packet is not ACK.

3. The receiver can reorder packets according to their serial numbers.

The sequence number plays a very important role in TCP connection. As part of the TCP connection, the initial sequence number also needs to be initialized during the three-way handshake. Since both sides of the TCP connection need to obtain the initial sequence number, Therefore, they need to send each other a SYN control message with the desired initial sequence number (SEQ), which is acknowledged by an ACK control message and SEQ+1.

Number of communication

When we talk about the number of communications required for TCP to establish a connection, we often get stuck in the “Why does it take three communications to establish a connection, but not two?” “We don’t talk about” How about four, five handshakes? . Increase the number of TCP connection communication problems often do not have to discuss the necessity of our pursuit is actually with less number of communication boundary (in theory) to complete the exchange of information, which is why emphasized the use of “two handshake” there’s no way to establish a TCP connection, the use of “three-way handshake” is the minimum number needed to establish a connection.

conclusion

Let’s go back to our question at the beginning of this article. Why do you need three handshakes to establish a TCP connection? After analysis, the answer is very clear.

1. Prevent TCP communication between two parties from establishing an incorrect connection.

2. Reduce unnecessary resource consumption of communication parties;

3. Help the communication parties to obtain the initial serial number, which can ensure that the transmission of data packets is not lost, but also ensure their transmission sequence.

The reasons for not using the two and four handshakes are:

1. Two-handshake cannot avoid the initialization of historical incorrect connections, which wastes resources of the recipient.

2. Four-way handshake, TCP communication protocol design allows us to transmit ACK and SYN control information at the same time, reducing the number of communication, so there is no need to use more communication times to transmit the same information.

The resources

[^1]Why do we need a 3-way handshake? Why not just 2-way?

Networkengineering.stackexchange.com/questions/2…

[^2]rfc793

Datatracker.ietf.org/doc/html/rf…

[^3] Why does a TCP connection need a three-way handshake

Draveness. Me/whys – the – DE…

[^4] Computer network – Transport layer

Github.com/CyC2018/CS-…