[Programming Basics] three handshakes and four waves in the TCP protocol (illustration), programming basics tcp

Source: Internet
Author: User

[Programming Basics] three handshakes and four waves in the TCP protocol (illustration), programming basics tcp

TCP establishment requires three handshakes to be established, and disconnection requires four handshakes. The entire process is shown in:

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.

How to disconnect? The simple process is as follows:


[Note] the interrupted connection end can be either the Client or the 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 do not have to rush to close the Socket, you can continue to send 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 ,"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, the Server can re-transmit it.. ", After the Server receives ACK ,"The connection can be disconnected.". If the Client still does not receive a reply after waiting for 2MSL, it indicatesThe Server has been shut down normally. 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 process: 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 resend. 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 the SYN + ACK packet. 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 likely that the SOCKET is not closed immediately. Therefore, only one ACK packet can be returned to the Client, "I have received your FIN message ". I can send FIN packets only 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 pass through 2MSL (maximum message segment survival time) to return to the CLOSE status?

A: Although we can directly go to the CLOSE status after sending all the four reports, we must assume that the network is unreliable and the last ACK can be lost. Therefore, the TIME_WAIT status is used to resend the possibly lost ACK packets.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.