Say hello politely - TCP protocol three-way handshake

Say hello politely - TCP protocol three-way handshake


We just want to introduce how computers on the network greet each other politely and how the TCP protocol establishes connections.

art of communication

When we communicate with others, what is the most basic and important thing?

Is it a good way to communicate? Is it just the right time? Is it the ability to put yourself in other people’s shoes?

Yes, but not entirely. The first step in communication is of course to say hello politely.

Wait a minute, what are we doing now, spreading knowledge about psychology? This is not our purpose and not our strength.

We just want to introduce how computers on the network greet each other politely and how the TCP protocol establishes connections.

TCP protocol three-way handshake

In the article Detailed Explanation of TCP Protocol Messages, the content of TCP messages has been introduced in detail. So in the TCP protocol, how is a connection established between the host and the host?

First of all, the host requesting the connection is called the client, and the connected host is called the server.

first handshake

When the client requests the server to establish a connection, it will send a data packet with the sequence number j (seq=j) and the control bit SYN=1 to the server. At this time, the client status is SYN_SENT.

second handshake

After the server receives the data packet, it will add an entry for the client's SYN packet (seq=j) in the unconnected queue, indicating that the client's data packet has been received. And the server will send a data packet with sequence number k (seq=k), confirmation number j+1 (ack=j+1), control bit SYN=1, and ACK=1 to the client. end, waiting for confirmation from the client, at this time the server status is SYN_RECV.

third handshake

After the client receives the data packet of SYN=1, ACK=1, ack=j+1, seq=k from the server, it will respond with a data packet of ACK=1, seq=j+1, ack=k+1 To the server, it means that the client has received the data packet from the server and entered the ESTABLISHED state, indicating that the connection has been established. After the server receives the ACK packet, it will delete the corresponding entry in the corresponding unconnected queue and enter the ESTABLISHED state, indicating that the connection has been established.

Retry and Fault Tolerance

Why does the TCP protocol have to perform a 3-way handshake, but 2 times can't do it?

First of all, it is assumed that only two handshakes are performed, that is, the server establishes a connection immediately after receiving the SYN data packet, and starts to transmit data. What is the problem?

If the server receives the SYN data, it immediately establishes a connection for the corresponding client. However, if the client does not receive the ACK packet from the client due to some reasons (such as network interruption, etc.), it requests to re-establish the connection and sends a request to the client. The server resends the SYN packet. At this time, the connection resource needs to be recreated on the server side, but the old connection is actually an invalid connection, which wastes system resources. If a large number of clients retry for a long time, it will cause a serious waste of server resources.

Summarize

In the TCP protocol, in order to ensure the reliability of the connection in the Internet, a three-way handshake mechanism is adopted. In order to prevent frequent establishment of invalid connections and waste of server resources due to interruption of established connections. In the first handshake, the server first maintains an unconnected record table, and finally the connection is successfully established, and then deletes the data in the unconnected record table.