The front face handshake three times and waved four times via TCP


First understand the characteristics of TCP

Features of TCP

  1. TCP is connection-oriented
  2. TCP provides reliable services: data is free of errors, loss, repetition, and arrives in order
  3. TCP is byte stream oriented
  4. TCP is one-to-one

TCP three-way handshake

TCP flag

Before understanding the TCP three-way handshake process, you need to understand the key flags of TCP

  1. SYN, abbreviated as S, synchronization flag bit, used to establish a session request and synchronize the serial number;
  2. ACK, abbreviated as ., confirm the flag bit, confirm the received data packet;
  3. FIN, abbreviated as F, complete flag, indicating that the transfer is complete and the connection will be closed

If SYN=1 and ACK=0, it means a "connection request" from the sender;
if SYN=1, ACK=1, it means the receiver "agrees to accept the connection request".

TCP three-way handshake diagram

Insert picture description here

TCP three-way handshake description

The first handshake : the client actively opens the connection , sets the TCP packet flag SYN to 1, and randomly generates a sequence number value seq=x (SYN=1, ACK=0, seq=x, request connection), and sends data Package to the server. After sending, enter the SYN-SENT state; the
second handshake : the server receives the data packet with the flag bit SYN=1 and ACK=0, and learns that the client requests to establish a connection. The server sets the position of the flag as: SYN=1, ACK=1, ack=x+1, and randomly generates the sequence number value seq=y; sends the data packet to the client and agrees to establish a connection. Enter the SYN-RCVD state.
The third handshake : The client has been confirmed by the server. Check if ACK is 1, and ack is x+1. If it is, then ACK=1, ack=y+1. The server then checks whether ACK is 1, and ack is y+1. If it is correct, the connection channel is established.

Why do you need three handshake

If you don't want the third handshake : As long as A sends a request and B accepts the request, the connection is established.
Why do we need to shake hands for the third time?
For example:
the first connection request message sent by client A, for some reason, stays for a long time. Until the second connection request from client A arrives, then server B will think that A has sent another connection request, so it accepts A's first connection request and confirms the connection. Because the connection can be established only through the first two handshake. However, B has been waiting for A to send data, and A does not actually send data, which causes a waste of B's ​​resources.
If there is a third handshake:
A sends a connection request to B, B confirms the connection, and A's reconfirmation is needed before the connection can be established. In the above example, if there is a third handshake, then B cannot be reconfirmed by A to establish this connection, and the connection will not be established. This time the connection request is invalidated.

TCP waved four times

Illustration of TCP wave four times:

Insert picture description here

TCP waved four times to explain

The release of the TCP connection requires four waves of hands. The example in the figure above is that the client actively requests to release the connection, and the server passively accepts the release of the connection.
First wave: The client wants to release the connection. Make FIN=1, which means that the connection is requested to be released, and seq=x is randomly generated; sent to the client, and enters the FIN-WAIT-1 phase (half-closed state, stopping the client from sending data to the server). But in fact, the client can also send an ACK confirmation message to the server.
The second wave: The server receives a request from the client to release the connection. Let ACK=1, ack=x+1, and randomly generate seq=y. Sent to the client, it means "received the client's request to release the connection". The server enters the CLOSE-WAIT phase.
The client receives the message and enters the FIN-WAIT-2 phase.

The first two handshake: the client let the server know: I want to release the connection; the server let the client know: I know you want to release the connection. So you can confirm that the connection from the client to the server is closed, and the release of the connection in one direction is completed!

The third wave of the hand: When the second wave of the hand sends a message to the client, the server enters the CLOSE-WAIT phase. When the CLOSE-WAIT is over, the server is ready to release the "server-to-client direction" connection. Then send a message with FIN=1 and ACK=1, indicating that the connection is ready to be released. ack=x+1, seq=z; Then, the server enters the LAST-ACK phase. Stop sending data from the server to the client, but can accept data from the client.
Fourth wave: The client sends a message: ACK=1 means it has received "The server is ready to release the connection".ack=z+1,seq=w;.

Why wave four times

Because TCP is in full-duplex mode, communication can be carried out in both directions, so four waves of hands are required to close the connection in both directions.

Wait for 2MSL in the TIME-WAIT phase

2MSL: is the maximum lifetime of a message segment, which is the longest time that a message segment can exist in the network before it is discarded.
Why wait for 2MSL?

  1. Ensure that the TCP full-duplex connection can be reliably closed;
    probably due to network reasons, the server did not receive the ACK message for the fourth wave of the client. Then the server will resend the FIN after the timeout (and wave again for the third time). If the client does not wait for 2MSL and the client enters the CLOSED phase directly, the client cannot receive the resent FIN. As a result, the release of the full-duplex connection cannot be completed. Therefore, it is necessary to wait for 2MSL to ensure that if the FIN is re-sent, the FIN signal can be received and a response will be given to complete the closure of the full-duplex connection.
  2. Ensure that the data of this connection disappears in the network.
    If the client directly enters the CLOSED phase, it will directly enter the CLOSED phase without waiting for the disappearance of the segment data of this connection. Then, if the client initiates a new connection, it is possible that the remaining data from the last connection is mixed in the new connection, causing the new connection to receive dirty data. Therefore, it is necessary to wait for 2MSL to ensure that the data of this connection is discarded in the network to avoid dirty data in the next connection.

Guess you like

Origin blog.csdn.net/qq_43263320/article/details/113781553