[Easy to understand] Three-way handshake and four-way wave

1. Three-way handshake

Three-way handshake (Three-way Handshake) means that when establishing a TCP connection, the client and the server will send a total of three message segments.

Initially both the client and the server are in the CLOSED state, and when the server application creates a listening socket, the server is in the LISTEN state.

1. The first handshake: the client sends a SYN message segment to the server, the flag bit SYN in the header of the message segment is set to 1, and also indicates its own initialization sequence number seq=x, at this time the client is in SYN_SENT state.

2. The second handshake: After the server receives the SYN segment, it will respond with its own SYN-ACK message. The header of the response message has three important information: first, SYN is set to 1; second, the confirmation number field ack=x+1; finally, the server chooses its own initial sequence number seq=y. The message segment indicates: "I have received your request to establish a connection, and the initial sequence number of the request message is x (the confirmation number ack=x+1 indicates that I have received the message with the initial sequence number seq=x), I agree to establish this connection, and my initial sequence number is y." At this time, the server is in the SYN_RCVD state.

3. The third handshake: After receiving the SYN-ACK message, the client will send an ACK message segment. The sequence number in this message segment is seq=x+1, and the confirmation number ack=y+1, indicating that I have received the SYN-ACK message. Here comes your confirmation. At this point the client is in the ESTABLISHED state.

After the server receives the ACK message, it is also in the ESTABLISHED state. At this time, the two parties have established a link.

It should be noted that: the first handshake and the second handshake only consume a serial number, but cannot carry data; the third handshake can carry data .

insert image description here

The role of the three-way handshake? (Why a three-way handshake? Wouldn't a two-way handshake work?)

1.1 The role of the three-way handshake:

(1) Confirm whether the receiving and sending capabilities of both parties are normal.
(2) Specify your own initialization sequence number to prepare for reliable transmission later.

1.2 What is the purpose of the three-way handshake ?

The first handshake : the client sends a message to the server, telling the server: "My client wants to establish a connection with you".

After receiving the first segment of the message, the server concludes that the sending function of the client is normal, and the receiving function of the server is normal.

The second handshake : After receiving the message, the server replies a message to the client, telling the client: "My server has received your request and agrees to establish a connection with you."

After receiving the second segment of the message, the client draws a conclusion: the sending and receiving functions of the client are normal, and the sending and receiving functions of the server are also normal. ( But at this time, the server cannot confirm whether the receiving function of the client and its own sending function are normal ).

The third handshake : After receiving the second segment of the message, the client sends a message back to the server, telling the server: "My client has received your reply and knows that you agree to the connection, so let's start the connection!"

After receiving the third segment of the message, the server draws a conclusion: the receiving function of the client and the sending function of the server are also normal.

So when the server receives the third message, the two sides establish a TCP connection.

1.3 Why is the two-way handshake not working?

Then it is obvious why the two handshakes do not work. If only the first two handshakes are used, the server cannot confirm whether the message segment it replies has been received by the client, and does not know whether its sending function and the receiving function of the client are normal.

2. Wave four times

Four times of waving means that when the client disconnects from the server, it needs to send a total of four segments to complete the disconnection of the TCP connection.

Initially, both the client and the server are in the ESTABLISHED state. If the client initiates a disconnection request (the server can also initiate it), the four-wave process is as follows:

1. The first wave : the client sends a FIN message segment, and the serial number seq=u is specified in the message segment . At this point the client is in the FIN_WAIT_1 state.

2. The second wave : After receiving the FIN message, the server immediately sends an ACK message segment, the confirmation number is ack=u+1, and the sequence number is set to seq=v . Indicates that the message from the client has been received. At this point the server is in the CLOSE_WAIT state.

During the period between the second wave and the third wave, data can still be transmitted from the server to the client due to the half-closed state.

3. The third wave : If the data transmission is completed and the server wants to disconnect, then a FIN message is sent, and a sequence number seq=w is re-designated, and the confirmation number is still ack=u+1 , indicating that the connection can be disconnected .

4. The fourth wave : After the client receives the message, it also sends an ACK message segment to respond. The sequence number of the message segment sent by the client last time is u, so the sequence number this time is seq=u+1. The confirmation number is ack=w+1 . At this time, the client is in the TIME_WAIT state, and it will enter the CLOSED state after a period of time to ensure that the server receives its own response message.

After the server receives the ACK message, it closes the connection and is also in the CLOSED state.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-C1Vxa8bt-1635066684897) (C:\Users\15921\AppData\Roaming\Typora\typora-user-images\ image-20211024164019559.png)]

2.1 Why do you need to wave your hand four times?

This question can be asked in another way, that is: why can't the middle two steps be combined? As long as the server receives the FIN message from the client, can it send ACK message and FIN message at the same time, can it be disconnected by waving three times?

The answer is usually not, because the trigger timing of ACK and FIN is different . We have to figure out one thing: the server can send an ACK message immediately after receiving the FIN message, indicating that my server has received your message; but if the server wants to send a FIN message, it needs to wait until the data in the receiving buffer is processed Only after. So waving takes four times.

2.2 Why wait for 2MSL? (Why is there a TIME_WAIT state?)

MSL (Maximum SegmentLifetime), which is the maximum time in the network before any segment is discarded, and the message will be discarded after this time. Waiting for 2MSL is to ensure that the server has received the last ACK message.

How to ensure that the server has received the last ACK message?

If the server does not receive the last ACK message , it will trigger a timeout retransmission: the server will send the FIN ACK message again. Then within 2MSL, the client will receive the FIN message again, and the client will know that the ACK just sent is lost and needs to be sent again.

If the server receives the last ACK message , the client will not receive any message within 2MSL, and the client will know that the ACK message just sent is not lost, and there is no need to send it again, and it can enter the CLOSED state with peace of mind. .

Guess you like

Origin blog.csdn.net/m0_52373742/article/details/120927298