This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
In the last article, we learned that a TCP channel requires three handshakes. In this article, we are going to talk about the process of destroying the TCP channel.
Explain the TCP four wave waving process in detail
First of all, a diagram of the four waves gives us a picture of the whole process
Both communication parties can disconnect the communication. This section uses the client to disconnect the communication
- Step 1: Both the client and the server are in the ESTABLISHED state. The client sends a TCP packet with the FIN field 1 in the header to the server, and the client state changes to FIN_WAIT_1
- The second step, the service side after receiving the FIN of the client sends a message, this is the first wave, the service side knew the client ready to close the connection, will first send a ACK response message, at this time to inform the client I have to know to close the connection, but this will not immediately disconnected, because the server may also request the pending, Therefore, the connection state is kept, and the server state changes to CLOSED_WAIT
- Step 3: After the client receives the ACK packet sent by the server, this is the second wave and the status changes to FIN_WAIT_2
- Step 4: After the server finishes processing all pending requests, it sends a FIN packet to the client indicating that the connection can be closed and the server status changes to LAST_ACK
- Step 5: After receiving the FIN packet from the server, the client sends an ACK packet to the server for the third wave. The status changes to TIME_WAIT
- Step 6: the client waits for 2MSL. If it does not receive any FIN packets from the server, the client closes the connection and is in the CLOSED state
- Step 7: After the server receives the ACK reply packet, this is the fourth wave, and the connection will be CLOSED and the whole process is ended
You may be wondering why TCP requires three handshakes and four waves of the hand. The answer is simple, waving, because after receiving the client FIN packet, can immediately reply, but could not handle the request, the connection could not immediately closed, so you need to wait until all the request processing is completed send FIN message, inform the client has to close the connection, due to an ACK and FIN was developed in the points, All the waving will be done one more time
After the client sends an ACK packet, why does it need to wait 2MSL before closing the connection?
Maximum Segment Lifetime (MSL) is defined as the Maximum Lifetime of a TCP packet during transmission. 2MSL indicates the total time required for the server to send a FIN packet and the client to send an ACK packet
If the server does not receive an ACK packet from the client within 1MSL, it resends a FIN packet. Therefore, the client delays the closed period of 2MSL. If no FIN packet is received within 2MSL, the server is proved to have correctly received the ACK packet from the client. If a FIN packet is received from the server within 2MSL, the ACK packet is lost and needs to be sent again. This delay ensures that both parties can safely close the connection
conclusion
On the TCP three handshakes and four waved on all finished, although these knowledge in the daily development will not directly bring benefits, but will be in the need to solve difficult diseases highlight power, so you can open the gap with others, become a really good front-end engineer, so, come on, SAO years!