Reference answer: We all know that TCP is connection-oriented. The three-way handshake is used to establish a connection, and the four-way handshake is used to disconnect the connection. Three-way handshakeFirst picture: Vernacular Comprehension
Let's take a look at the three-way handshake process:
A handshake
Second handshake
Three-way handshake
Well, after the three-way handshake process, the connection between the client and the server is confirmed to be normal, and then it enters the ESTABLISHED state, and the server and the client can communicate happily. Here is a graphic representation of the dynamic process: There is a small detail here. The third handshake can carry data, which is a point often asked in interviews. So why do we need three handshakes? Can't we just use two?
Since network transmission has delays (it has to pass through network optical fibers and various intermediate proxy servers), during the transmission process, for example, the client initiates the first handshake with SYN=1. If the server directly creates this connection and returns a data packet containing SYN, ACK, and Seq to the client, this data packet is lost due to network transmission reasons. After the loss, the client has not received the data packet returned by the server. If there is no third handshake to tell the server that the client has received the data transmitted by the server, the server does not know whether the client has received the information returned by the server. The server considers the connection to be available, and the port remains open. When the client re-issues a request due to a timeout, the server will reopen a port connection. As a result, many invalid connection ports will be opened in vain, resulting in a waste of resources. This process can be understood as: Another situation is that the request information sent by an invalid client is transmitted to the server for some reason. The server thinks it is a valid request sent by the client and an error occurs after receiving it. So we need a "third handshake" to confirm this process: The data of the third handshake tells the server whether the client has received the data sent by the server during the "second handshake" and whether the sequence number of this connection is valid. If the data sent is "received and no problem", the server will establish a TCP connection normally after receiving it. Otherwise, the TCP connection fails to be established and the server closes the connection port. This reduces server overhead and errors caused by receiving invalid requests. Data transferFour wavesVernacular Comprehension
In order to prevent the final ACK from being lost, you need to wait for a while after sending the ACK, because if the packet is lost, the server needs to resend the FIN packet. If the client has closed, the server will interpret the result as an error. As a result, a large number of ports will be occupied in high-concurrency non-long connections. Use Both parties can actively disconnect, and after disconnection, the "resources" in the host will be released. The above picture shows that the client actively closes the connection: A wave
Second wave
Wave three times
Four waves
A FIN and an ACK are required in each direction, so it is often called four waves. Why wave four times?
From the above process, we can see that the server usually needs to wait for the data to be sent and processed, so the server's ACK and FIN are generally sent separately, resulting in one more handshake than the three-way handshake. Why does the client have to wait for 2MSL in the TIME-WAIT phase? In order to confirm whether the server has received the ACK confirmation message sent by the client, when the client sends the last ACK confirmation message, it cannot be sure that the server can receive this message. Therefore, after sending the ACK confirmation message, the client will set a timer with a duration of 2MSL. MSL refers to Maximum Segment Lifetime: the maximum life cycle of a TCP message during transmission. 2MSL is the maximum length of time that the FIN message sent by the server and the ACK confirmation message sent by the client can remain valid. If the server does not receive the ACK message from the client within 1MSL, it will send a FIN message to the client again:
The client sends an ACK confirmation message to the server again, the timer is reset, and the 2MSL timing starts again.
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. Here is also a graphic representation of the dynamic process: The article comes from: Front-end Restaurant. If you want to reprint this article, please contact the Front-end Restaurant ReTech Toutiao account. github: https://github.com/zuopf769 |
<<: What is 5G voice like now?
>>: LoRaWAN for public, private and hybrid networks
It is globally recognized that 5G is the trend of...
After the rapid development in 2020, 2021 is a cr...
What is 5G Home Internet? 5G Home Internet, also ...
Embedded Subscriber Identity Modules (eSIMs) have...
The 2020 China Artificial Intelligence Summit For...
SoftShellWeb has released several special promoti...
[[225474]] Editor's Note In recent years, wit...
Cables and connectors have been an integral part ...
The tribe often shares information about DiyVM. T...
GPP defines network slicing as one of the main fu...
[51CTO.com original article] On September 23, 202...
GreenCloudVPS released an email about its 8th ann...
5G offers faster download speeds than previous ce...
On March 16, the "Network Security Pilot Dem...
[[426617]] After much anticipation, the long-awai...