Graphical Network: Uncover the principle behind TCP's four wave of hands, combined with the example of boyfriend and girlfriend breaking up
In this section, Ruige continues to show you the network and explore the principles behind TCP’s four waved disconnections.
Let's jump right in!
A brief review of TCP three-way handshake
TCP is a full-duplex protocol. In order to establish a connection, a reliable link can be established through a three-way handshake:
- First, the client will send a SYN message (short for SYNchronize) indicating that the client wants to establish a connection with the server.
- After receiving the SYN message from the client, the server replies with a SYN-ACK message, including a SYN sequence number and an ACK number.
- After receiving the SYN-ACK message from the server, the client sends an ACK message to the server.
Simple process of TCP three-way handshake
TCP waved four times
Examples in life
Both ends of the connection can terminate the TCP connection, and even unilateral termination is possible, which is also known as a half-open connection, even if one side has disconnected, the other side can still transmit data.
When we say waving four times, we mean that the two sides disconnect.
Let's explain it with an example from life:
Four Waves - Example from Life: Breaking Up
Xiaomei and Xiaoming are college classmates. When they first went to college, Xiaoming was tall and handsome and could play basketball. Xiaomei admired him very much, and Xiaoming also liked Xiaomei very much. In this way, on a dark and windy night, they became friends. couple.
However, with the passage of time and changes in the environment, the two gradually lost their enthusiasm, so Xiaomei proposed to Xiaoming to break up. The process of breaking up is similar to TCP waving four times:
- The first time: Xiaomei said to Xiaoming, "Xiaoming, we have been together for so long, I think we are not suitable, let's break up"
- The second time: Xiao Ming was not too surprised after hearing this, but he couldn't agree directly, or he would become a scumbag, so he waved his hand for the second time, and he politely said, "No, I still have something to say. Let me tell you, you are actually pretty good, not only are you beautiful, but also have a good personality, you are also reasonable, and you study very hard, and I am very happy to meet you in college!"
- The third time: Xiaoming's beeping is almost the same. It's time to show his true colors. Then he said to Xiaomei, "Since you have made up your mind, it's no fun for me to pester you, so let's break up peacefully~"
- The fourth time: Xiaomei was very angry after hearing Xiaoming's whole bunch of hypocritical words, so she said, "That's it, delete each other's WeChat, goodbye!"
Then a big red exclamation mark will appear on Xiao Ming's WeChat!
WeChat block
This is four times of waving, isn't it very vivid?
Next time, if you forget the steps of TCP waving four times, I think you can imagine yourself as a scumbag
Well, let's get down to business, let's analyze the principle of TCP's four waves.
Analysis of the Principle of Four Waves
Four wave process
As shown in the figure, the client wants to disconnect from the server:
- The client will send a FIN message, FIN is the abbreviation of FINISH, the purpose of the FIN message is to enable TCP to gracefully terminate the established connection, and then the client enters a state called the FIN-WAIT state. In the FIN-WAIT state, The client continues to receive messages from the server and continues to process messages already in the queue, but the client does not send any additional data.
This can be imagined. When Xiaomei was sending the breakup message, her boyfriend Xiaoming kept sending messages to Xiaomei. The messages that Xiaomei didn’t read entered the queue. Xiaomei would still read the messages in the queue, but not Respond to these messages proactively.
- The server sends an ACK message to the client to confirm that he has received the end message. At this time, the server will no longer accept data from the client.
- The server can continue to send data to the client (the last thing to do), and if the server has no more data to send, it will also terminate the connection by sending a FIN message.
- Then the client sends an ACK to the server to confirm that it has received the message that the server has actively disconnected.
At this point, the TCP connection is disconnected!
TCP waved four times - with serial number
As shown in the figure, M and N are both serial numbers, which represent the client and the server respectively, which is also to distinguish the server and the client. For example, when you see M, you think that the message comes from the client, and when you see N, you think that the message comes from Server.
So the process becomes:
- The client sends FIN M to the server to the server.
- The server sends ACK M+1 to the client, and when the client receives M+1, it knows that the server has received its Finish message.
- When the server sends FIN N to the client, it means that the server actively sends the end message to the client.
- The client replies with an ACK N+1 message to the server, and the server sees its own N, so it knows that the client has also received its own end message.
At this point, the TCP has been successfully disconnected.
Maximum lifetime of disconnection
Here is another example. After Xiaomei sends the message "That's it, delete WeChat, goodbye!", she will wait up to four minutes to give Xiaoming a chance to stay:
wait four minutes
If Xiao Ming didn't hold back within these four minutes, then I'm sorry, let's end it completely! Game Over!
In the [Analysis of Four Waves Principle], we noticed the TIME_WAIT state. After the client sends the last ACK segment, the connection will not be terminated immediately. Since there is no guarantee that the last sent data packet has indeed arrived, the client or server will initially Keep the time waiting state until the maximum life cycle (MSL, Maximum Segment Lifetime) of the ACK segment and any new FIN segment can completely enter the CLOSED state.
This mechanism is mainly for two reasons:
- Prevents delayed data segments from being received by other TCP connections using the same source address, source port, destination address, and destination port.
- Ensure that the TCP connection is properly closed remotely, that is, the party waiting for the passively closed connection to receive the message corresponding to the ACK.