TCP’s "three-way handshake" and "four-time wave" (why does TCP need to shake hands three times and wave four times)

Back to the higher level: Computer Network Chapter 5 Transport Layer

1.seq, ack, ACK, SYN, FIN

(1) Sequence number: Seq sequence number, occupies 32 bits, used to identify the byte stream sent from the TCP source to the destination, and the initiator will mark this when sending data.

(2) Acknowledgement number: Ack serial number, occupies 32 bits 只有ACK标志位为1时,确认序号字段才有效,, Ack=Seq+1.

(3) Flags: There are 6 in total, namely URG, ACK, PSH, RST, SYN, FIN, etc. The specific meaning is as follows:

  • ACK: 确认序号有效
  • SYN: initiate a new connection.
  • FIN: Release a connection.

2. Three handshake

Insert picture description here
Insert picture description here

1.2 Why does TCP need a three-way handshake:

The main purpose is to prevent the invalid connection request segment from being suddenly transmitted to B, resulting in an error.
Insert picture description here
Insert picture description here
Why does tcp need a three-way handshake to establish a connection?

3. Wave four times

Insert picture description here
3.1 Why are there three "handshakes" but four "waves"?

The reason why TCP only needs the "three-way handshake" when establishing a connection is because in the second "handshake" process, the TCP message sent by the server to the client is marked with SYN and ACK. SYN is a request for connection flag, indicating that the server agrees to establish a connection; ACK is an acknowledgement message, indicating that the server has received its request message.

That is, the SYN connection establishment message and the ACK confirmation message are transmitted in the same "handshake", so the "three-way handshake" is not more or less, just so that the two parties clearly communicate with each other.

The reason why TCP needs to "wave hands four times" when releasing the connection is because the FIN release connection message and the ACK confirmation message are transmitted by the second and third "handshake" respectively. Why are they transmitted together when the connection is established, but separately when the connection is released?

  • When establishing a connection, the passive server ends the CLOSED phase and enters the "handshake" phase without any preparation. It can directly return SYN and ACK packets to start the connection.
  • When the connection is released, the passive server cannot immediately release the connection when it suddenly receives a request from the active client to release the connection. Because there is still necessary data to be processed, the server first returns an ACK to confirm the receipt of the message, and passes through CLOSE-WAIT After the phase is ready to release the connection, the FIN release connection message can be returned.

So it was "three handshake" and "four wave hands".

3.2 Why does the client wait for 2MSL in the TIME-WAIT phase?

To confirm whether the server has received the ACK confirmation message from the client

When the client sends the final ACK message, it is not certain that the server can receive the message. Therefore, after the client sends the ACK confirmation message, it will set a timer with a duration of 2MSL. MSL refers to Maximum Segment Lifetime: the maximum life cycle of a TCP packet in the transmission process. 2MSL is the maximum length of time that the server-side FIN message and the client-side ACK confirmation message can remain valid.

If the server does not receive the ACK confirmation message from the client within 1MSL, it will send a FIN message to the client again;

  • If the client is within 2MSL and receives the FIN message from the server again, it means that the server did not receive the ACK confirmation message from the client for various reasons. The client sends an ACK confirmation message to the server again, the timer is reset, and the 2MSL timing is restarted;
  • Otherwise, the client does not receive the FIN message from the server again within 2MSL, indicating that the server has received the ACK confirmation message normally, and the client can enter the CLOSED phase and complete the "four waves".

Therefore, the client has to go through the TIME-WAIT phase of 2SML; this is why the client enters the CLOSED phase later than the server

Reference blog:

Explain the "Three Handshake" and "Four Waves" of TCP Connection

Guess you like

Origin blog.csdn.net/baidu_40537062/article/details/107984208