Python Network Programming-TCP protocol three handshake and four waves (illustration)

Source: Internet
Author: User
Tags ack

Establishing TCP requires three handshakes to be established, while disconnecting requires four handshakes. The entire process is as follows:

Let's take a look at how to make a connection.

First, the client sends the connection request message, the Server section accepts the connection and replies to the ACK message, and allocates resources for this connection. An ACK message is also sent to the server segment after the client receives an ACK message, and the resource is allocated so that the TCP connection is established.

How do I disconnect it? The simple process is as follows:

"Note" The interrupt connection can be either client side or server side.

Suppose the client side initiates an interrupt connection request, which is to send a fin message. After the server receives the fin message, it means " my client has no data to send to you ", but if you have data that is not sent, you do not need to close the socket, you can continue to send data. So you first send an ACK, " tell the client that your request I received, but I am not ready, please continue to wait for my message ." At this point the client enters the fin_wait state and continues to wait for Fin messages on the server side. When the server side determines that the data has been sent, the fin message is sent to the client side, " tell the client side, OK, my side of the data is finished, ready to close the connection ." Client side received fin message, " I know can shut down the connection, but he still do not believe the network, afraid the server side do not know to shut down, so send an ACK into the time_wait state, if the server does not receive an ACK can be re-transmitted . "When the server side receives an ACK," You know you can disconnect . " Client side waiting for 2MSL still not received a reply, the server side has been properly shut down, well, I can also close the client terminal connection . The OK,TCP connection is closed like this!

The client side of the process is experiencing the following status:

And the server side experience the process is as follows: Reprint please specify: Blog.csdn.net/whuslei

"Note" in the time_wait state, if the last ACK sent by the TCP client is lost, it will be re-sent. The time required in the TIME_WAIT state is dependent on the implementation method. Typical values are 30 seconds, 1 minutes, and 2 minutes. The connection is formally closed after waiting, and all resources (including the port number) are released.

"Question 1" Why is the handshake three times when it is connected and four times when it is closed?
A: The syn+ack message can be sent directly after the server receives the SYN connection request message from the client side. Where the ACK message is used to answer, the SYN message is used for synchronization. However, when the connection is closed, when the server side receives the fin message, it is likely that the socket will not be closed immediately, so you can only reply to an ACK message, tell the client side, "You send fin message I received." I can't send fin messages until all the messages on my server end are sent, so I can't send them together. Therefore, four-step handshake is required.

"Question 2" Why does the time_wait state need to go through 2MSL (maximum message segment lifetime) to return to the close state?

A: Although according to reason, four messages are sent, we can go directly to the close state, but we must pretend that the network is unreliable, there can be a last ACK lost. So the TIME_WAIT state is used to re-send the possible missing ACK messages.

Python Network Programming-TCP protocol three handshake and four waves (illustration)

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.