The process of establishing a connection with the TCP protocol----analysis of the three-way handshake

Table of contents

foreword

TCP establishes a connection---three-way handshake

The process of establishing a connection

 Meaning of TCP status

client

server

The meaning of the three-way handshake


foreword

We know that one of the reliability of the TCP protocol is to maintain reliability by establishing a connection. Before the TCP protocol sends data, the two parties need to establish a connection. After the transmission is completed, the two communicating parties need to disconnect. This is the resume of the TCP connection. and terminated.

The TCP connection is divided into three post-stages in total, the three stages of connection establishment -> data transmission -> and disconnection.

What we mainly discuss is the connection establishment phase, followed by a very typical TCP connection establishment process.

The three-way handshake means that the server and the client need to send three data packets, exchange the initial serial numbers of both parties, and confirm that the receiving/sending capabilities of both parties are normal, and a connection can be established.

TCP establishes a connection---three-way handshake

The process of establishing a connection

The TCP three-way handshake is the process of establishing a TCP connection, initiated by the client. The following is the analysis of the TCP three-way handshake:

The first handshake: the client sends a SYN packet (SYN=1, ACK=0) to the server, requesting to establish a connection. The client randomly selects an Initial Sequence Number (ISN), which is used for sequence number counting in subsequent communications.

The second handshake: The server receives the SYN packet from the client. If it agrees to establish a connection, it should send a SYN+ACK packet (SYN=1, ACK=1), where SYN=1 means to confirm the client’s SYN packet, and ACK=1 means Confirm receipt of the serial number of the client (ie ISN+1), and also randomly select a serial number as the initial serial number of the server.

The third handshake: the client receives the SYN+ACK packet from the server, and sends an ACK packet (SYN=0, ACK=1) to the server, indicating that it has received the SYN+ACK packet from the server, and also confirms the serial number of the server (ie the server's ISN+1).

So far, the TCP three-way handshake is completed, and the TCP connection is established. After the connection is established, data communication can begin.

 Meaning of TCP status

client

a. CLOSED: Initial state, indicating that the client has not initiated a connection.

b. SYN-SENT: The client sends a SYN packet and waits for the confirmation from the server.

c. ESTABLISHED: The client and server have completed the three-way handshake, the connection has been established, and data transmission can be performed.

server

a. CLOSED: Initial state, indicating that the server has not received the connection request from the client.

b. LISTEN: The server is listening to the client's connection request.

c. SYN-RECEIVED: The server has received the SYN packet from the client and is waiting to send a SYN+ACK packet for confirmation.

d. ESTABLISHED: The server and the client have completed the three-way handshake, the connection has been established, and data transmission can be performed.

This is a typical three-way handshake process, and the establishment of a TCP connection can be completed through the above three message segments. The purpose of the three-way handshake is not only to let the communicating parties know that a connection is being established, but also to use the option field in the data packet to exchange some special information and exchange the initial sequence number.

Generally, the first party to send a SYN message is considered to actively open a connection, and this party is usually also called a client. The receiver of the SYN is usually called the server, which is used to receive the SYN and send the following SYN, so this opening method is passive opening.

TCP needs three segments to establish a connection, but four segments to release a connection.

The meaning of the three-way handshake

I think one of the meanings of the three-way handshake is to confirm that the ability of the server and the client to receive and send data is normal. The "three times" of the three-way handshake is actually the minimum amount of data packets required to establish a connection. The server and the client It takes at least three times to confirm that the receiving/sending capabilities of both parties are no problem, and the connection can be established and data can be sent.

Although more handshake times are still possible, such a cumbersome process will reduce the efficiency of data transmission even more. Therefore, the " three times" of the three-way handshake is the best number. If it is less than three times, a stable connection cannot be established. Less than three times , it is not necessary, it will reduce the efficiency of transmission , so the three-way handshake is the best.

If the number of handshakes is reduced to two, the following may result:

  1. During the first handshake, the client sends a SYN packet to the server, but due to network delays and other reasons, the SYN packet is delayed. At this time, the client thinks that the connection has been successfully established and can start sending data packets.

  2. During the second handshake, the server sends a SYN+ACK packet to the client after receiving the SYN packet from the client. Reply, it will consider that the connection establishment failed, and resend the SYN packet.

  3. In the third handshake, the client receives the SYN+ACK packet from the server, but the ACK packet is lost. At this time, the server will think that the connection establishment has failed, and will resend the SYN+ACK packet, which will lead to the establishment of multiple connections. And problems such as repeated data packets.

Therefore, the TCP three-way handshake is designed to ensure reliable data transmission connections and is necessary.

Guess you like

Origin blog.csdn.net/asdasd121312dasd/article/details/131946375