Three-way handshake and four-way wave - a must-see for Xiaobai

Table of contents

1. Three-way handshake

2. Wave four times

3. Analysis of 11 state nouns

The essence of TCP's three-way handshake and four-way wave is the connection and disconnection of TCP communication.

Three-way handshake: In order to track and negotiate the amount of data sent each time, ensure that the sending and receiving of data segments are synchronized, confirm data sending according to the amount of data received, when to cancel the connection after receiving, and establish a virtual connection.

Wave four times: Terminate the TCP connection, which means that when a TCP connection is disconnected, the client and the server need to send a total of 4 packets to confirm the disconnection of the connection.

Sequence diagram of TCP three-way handshake and four-way wave

1. Three-way handshake

The TCP protocol is located at the transport layer, and its role is to provide reliable byte stream services. In order to accurately deliver data to the destination, the TCP protocol adopts a three-way handshake strategy.

Three-way handshake​​​​​​Principle:

The first handshake: the client sends a packet with the SYN (synchronize) flag to the server;

The second handshake: After the server receives it successfully, it returns a packet with the SYN/ACK flag to deliver confirmation information, indicating that I have received it;

The third handshake: the client sends back a data packet with the ACK flag, indicating that I know, and the handshake ends.

Among them: the SYN flag is set to 1, which means that the TCP connection is established; the ACK flag means the verification field.

The three-way handshake can be understood through the following interesting diagram:

Detailed description of the three-way handshake process:

1. The client sends a request message to establish a TCP connection, which contains a seq sequence number, which is randomly generated by the sender, and sets the SYN field in the message to 1, indicating that a TCP connection needs to be established. (SYN=1, seq=x, x is a randomly generated value);

2. The server replies to the TCP connection request message sent by the client, which contains the seq sequence number, which is randomly generated by the reply end, and sets SYN to 1, and generates an ACK field. The value of the ACK field is sent by the client Add 1 to the previous serial number seq to reply, so that when the client receives the information, it knows that its TCP establishment request has been verified. (SYN=1, ACK=x+1, seq=y, y is a randomly generated value) The ack plus 1 here can be understood as confirming who to establish a connection with;

3. After receiving the TCP establishment verification request sent by the server, the client will add 1 to its serial number, and reply to the ACK verification request again, and add 1 to the seq sent by the server to reply. (SYN=1, ACK=y+1, seq=x+1).

2. Wave four times

Since TCP connections are full-duplex, each direction must be closed separately. The principle is that when one party completes its data sending task, it can send a FIN to terminate the connection in this direction. Receiving a FIN only means that there is no data flow in this direction, and a TCP connection can still send data after receiving a FIN. The party that shuts down first will perform an active close, while the other party will perform a passive close.

Wave four times​​​​​​Principle:

The first wave: the client sends a FIN to close the data transmission from the client to the server, and the client enters the FIN_WAIT_1 state;

The second wave: After receiving the FIN, the server sends an ACK to the client, confirming that the sequence number is the received sequence number + 1 (same as SYN, one FIN occupies one sequence number), and the server enters the CLOSE_WAIT state;

The third wave: the server sends a FIN to close the data transmission from the server to the client, and the server enters the LAST_ACK state;

The fourth wave: After the client receives the FIN, the client t enters the TIME_WAIT state, and then sends an ACK to the server, confirming that the serial number is the received serial number + 1, and the server enters the CLOSED state, and completes four waved hands.

Among them: the FIN flag is set to 1, which means disconnecting the TCP connection.

The four waves can be understood through the following interesting illustrations​​​​​​:

Four waves​​​​​​The process is detailed:

1. The client sends a request message to disconnect the TCP connection, in which the message contains a seq sequence number, which is randomly generated by the sender, and also sets the FIN field in the message to 1, indicating that the TCP connection needs to be disconnected . (FIN=1, seq=x, x is randomly generated by the client);

2. The server will reply to the TCP disconnection request message sent by the client, which contains the seq sequence number, which is randomly generated by the replying end, and will generate an ACK field. The value of the ACK field is the seq sequence number sent by the client. Basically add 1 to reply, so that when the client receives the information, it knows that its TCP disconnection request has been verified. (FIN=1, ACK=x+1, seq=y, y is randomly generated by the server);

3. After the server responds to the client's TCP disconnection request, it will not immediately disconnect the TCP connection. The server will first ensure that all the data transmitted to A has been transmitted before the disconnection. Once it is confirmed that the transmission of data is completed , the FIN field of the reply message will be set to 1, and a random seq sequence number will be generated. (FIN=1, ACK=x+1, seq=z, z is randomly generated by the server);

4. After the client receives the TCP disconnection request from the server, it will reply to the disconnection request from the server, including a randomly generated seq field and an ACK field. The ACK field will add 1 to the seq of the TCP disconnection request from the server, thus Complete the verification response requested by the server. (FIN=1, ACK=z+1, seq=h, h is randomly generated by the client)

Guess you like

Origin blog.csdn.net/wangshuaibinggg/article/details/129045009