Three handshakes are required to establish TCP connections, and four handshakes are required to disconnect connections. The entire process is shown as follows:
Let's take a look at how to establish a connection.
First, the client sends a connection request message. The server segment replies to the ACK packet after accepting the connection and allocates resources for the connection. After the client receives the ACK packet, it also generates an ACK packet to the server segment and allocates resources so that the TCP connection is established.
So how to disconnect? A simple procedure is as follows:
[Note] the interrupted connection end can be either a client or a server end.
Assume that the client initiates an interrupt connection request, that is, sending a FIN packet. After the server receives the fin message, it means"My client has no data to send to you.", But if you still have data not sent, you don't have to rush to close the socket to continue sending data. So you should first send ACK ,"Tell the client that I have received your request, but I am not ready yet. Please wait for your message". At this time, the client enters the fin_wait status and continues to wait for the server's Fin message. When the server determines that the data has been sent, it sends the fin message to the client ,"Tell the client. Okay. I have sent the data and are ready to close the connection.". After the client receives the fin message ,"He knows that the connection can be closed, but he still does not trust the network, for fear that the server does not know to close, so the ACK enters the time_wait status after being sent. If the server does not receive the ACK, it can be re-transmitted.. ", After the server receives ACK ,"The connection is disconnected.". If the client still does not receive a reply after waiting for 2msl, it indicatesThe server has been properly shut down. Well, my client can also close the connection.. OK. The TCP connection is closed!
The status of the client is as follows:
The server has gone through the following steps: reprinted, please note: blog.csdn.net/whuslei
Note]In the time_wait status, if the last ack sent by the tcp client is lost, it will send again. The time required in the time_wait status depends on the implementation method. The typical values are 30 seconds, 1 minute, and 2 minutes. After the connection is closed, all resources (including port numbers) are released.
[Question 1] Why is there a three-way handshake when the connection is closed, but a four-way handshake when the connection is closed?
A: When the server receives the SYN connection request from the client, it can directly send SYN + ACK packets. The ACK packet is used for response and the SYN packet is used for synchronization. However, when the connection is closed, when the server receives the FIN packet, it is very likely that the socket will not be closed immediately. Therefore, the server can only reply to an ACK packet and tell the client, "I have received your fin message ". I can only send FIN packets when all the packets on my server are sent. Therefore, I cannot send them together. Therefore, a four-step handshake is required.
[Question 2] Why does the time_wait status need to pass through 2msl (maximum message segment survival time) to return to the close status?
A: even though the four reports are successfully sent, we can directly go to the close state. However, we must assume that the network is unreliable and that the last ack can be lost. Therefore, the time_wait status is used to resend the possibly lost ACK packets.
Three handshakes and four handshakes in TCP protocol (illustration)