Why does TCP need a three-way handshake? Explain to you in the most popular words

2021.09.09

TCP implementation principles and why three-way handshake is needed? Two-way handshake is not possible? Four-way handshake is not possible? Readers can read the detailed explanation of this blog with questions.


TCP implementation principle and why three-way handshake is needed? Two-way handshake is not possible? Four-way handshake is not possible? Readers can read the detailed explanation of this blog with questions.


Ok, before explaining the reason, we still need to review the basic knowledge of TCP and the three-way handshake protocol:

 

1. What is the TCP protocol?

TCP: The Transmission Control Protocol translates to the Transmission Control Protocol. The TCP protocol is a connection-oriented, reliable, byte stream-based transport layer protocol

 

RFC 793 defines TCP connection:

Connections:

  •  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.

It roughly means that the TCP connection is used to ensure reliability and flow control mechanism, including Socket, serial number, and window size.

The Socket is composed of IP and port, the serial number is used to solve the problem of disorder, and the window size is used for flow control.

 

2. Characteristics of TCP protocol

Connection-oriented: refers to TCP is a protocol that connects the server and the client

Byte stream oriented: data communication between TCP server and client is transmitted through byte stream data

Reliable: It means that the data transmission between the TCP server and the client is very stable. Even if the network is poor, TCP can guarantee the data transmission to the receiver.


ps: The reliability of TCP transmission is due to the fact that TCP will record the sending status of information, which data has been received, which data has not been received, TCP will record it, and which packet loss conditions are the unsuccessful transmission conditions, TCP Will re-send packets, so the reliability of TCP is so guaranteed


3. The TCP three-way handshake execution process

The TCP three-way handshake execution process is a very common question in interviews, because this question is also a very important basis for computers, so it needs to be studied carefully


Keyword description:

SYN: Synchronize Sequence Numbers, synchronization sequence number

ACK: Acknowledge Character, confirmation character

SEQ: Sequence Number, sequence number

The TCP three-way handshake execution process:

  • First, both the server and the client are in the CLOSED state, and then the server starts, listens to the port, and the state becomes LISTEN (listening) state
  • In order to request resources, the client sends a connection, and sends a synchronization sequence number SYN. ​​At this time, the client becomes a SYN-SEND state.
  • After the server receives the client request, it sends SYN and ACK, and then the server state becomes SYN-RCVD state

After the client receives the information, it sends an ACK again, and then changes to the ESTABLISHED (confirmed) state. After the server receives the return information, the status also changes to the ESTABLISHED (confirmed) state.


4. Why does the TCP protocol need a three-way handshake?

Ok, after knowing the basic working principle of TCP's three-way handshake, you can explain why TCP needs three-way handshake? Why not design it as a two-way handshake?

Reason: avoid repeated connections

In fact, in RFC 793 Transmission Control Protocol, there is a reason for the three-way handshake.

 

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

Translated into Chinese roughly means that the main reason is to prevent the old repeated connection from causing connection confusion.

 

For example, in a complex network environment, the client may send multiple requests in a row. If it is only designed as a two handshake situation, the server can only keep receiving the request, and then return the request information, and it is not known whether the client's request is successful. These expired requests will cause confusion in the network connection.

 

Therefore, it is designed as a three-way handshake. After the client receives the return message from the server SEQ+1, it will know that the connection is a historical connection, so it will send a message to the server to tell the server.

 

So TCP is designed as a three-way handshake to avoid repeated connections.

 

Then it can be designed as a four-way handshake? Five-way handshake? No?

 

The answer is yes, but in order to save resources, the three-way handshake can meet the actual situation, so there is no need to design for four-way handshake, five-way handshake, etc.