Why TCP needs 3 handshake and 4 waves (reprint)

Source: Internet
Author: User

Why you need a "three-time handshake"

In the fourth edition of the "Computer network," The purpose of the "three handshake" is " to prevent the failure of the connection request packet suddenly transmitted to the server, resulting in an error". In another classic computer network, the purpose of the "three-time handshake" is to solve the problem of "repeated grouping of delays in the network". These two kinds of needless statements actually clarify the same problem.
The example in the Shehiren version of the computer network is that the "Failed connection request message segment" is generated in a situation where the first connection request message segment of the client is not lost, but is stuck in a network node for a long time, Delay until a certain time after the connection is released before the server arrives. originally this is a message segment that has already expired. However, after the server receives this failed connection request message segment, it is mistaken for a new connection request from the client. The client is then sent a confirmation message segment, agreeing to establish a connection. Assuming that the "three-time handshake" is not used, the new connection is established as soon as the server issues a confirmation. Because the client is now not making a connection request, the server acknowledgement is ignored and data is not sent to the server. But the server thought the new transport connection had been established and waited for the client to send the data. In this way, many of the server's resources are wasted. the use of "three-time handshake" method can prevent the above phenomenon. For example, in that case, the client does not issue confirmation to the server's confirmation. The server knows that the client does not require a connection because it cannot receive a confirmation. ”。 The main purpose is to prevent the server side from waiting and wasting resources.

Why do I need to "wave four times"?
There might be questions about why the ACK was sent with SYN when the TCP connection handshake, but the ACK was not sent with Fin. The reason is that because TCP is in full duplex mode , when you receive fin it means that no data is sent again, but you can continue to send the data.

Shake hands, wave the process of each State introduction:

3-Time handshake process status:
LISTEN: This is also very easy to understand a state, that the server side of a socket is listening state, can accept the connection.
Syn_sent: When the client socket performs a connect connection, it sends a SYN message first, so it will then enter the Syn_sent state and wait for the server to send the 2nd message in the three-time handshake. The Syn_sent status indicates that the client has sent a SYN message. (Sender side)

SYN_RCVD: This state and syn_sent thinking back echo this state to accept the SYN message, under normal circumstances, this state is the server side of the socket in the establishment of a TCP connection during the three handshake session in the process of a middle state, very short, Basically with netstat you are very difficult to see this state, unless you deliberately write a client test program, deliberately three times the TCP handshake process last ACK message is not sent. Therefore, when the ACK message is received from the client, it goes into the established state. (Server side)
Established: This is easy to understand, indicating that the connection has been established.

4 Waves Wave process status: (Can be referenced)


Fin_wait_1: This state should be well explained, in fact, the real meaning of fin_wait_1 and fin_wait_2 state is to wait for each other's fin message. The difference between the two states is: The fin_wait_1 state is actually when the socket in the established state, it would like to actively close the connection, send a FIN message to the other side, when the socket is entered into the fin_wait_1 state. And when the other party responds to the ACK message, then into the fin_wait_2 state, of course, under the actual normal circumstances, regardless of the circumstances of each other, should immediately respond to the ACK message, so fin_wait_1 state is generally more difficult to see, and Fin_wait_ 2 states can also sometimes be seen with netstat. (Active side)
Fin_wait_2: The above has explained in detail this state, in fact, the fin_wait_2 state of the socket, that is, the semi-connection, that is, one side requires close connection, but also tell the other side, I temporarily have a bit of data to send you (ACK information), and then close the connection. (Active side)
Time_wait:received a fin message from each other and sent out an ACK message, just wait for 2MSL to return to the closed available state. If the fin_wait_1 state, received the other side with the FIN flag and the ACK flag message, you can directly into the time_wait state, without having to go through the fin_wait_2 state. (Active side)
CLOSING (relatively rare): This state is relatively special, the actual situation should be very rare, belonging to a relatively rare exception state. Normally, when you send a fin message, it is supposed to receive (or receive) the other's ACK message before receiving the other's fin message. But closing status indicates that you send fin message, and did not receive the other's ACK message, but also received the other side of the fin message. Under what circumstances will this happen? In fact, it is not difficult to come to a conclusion: that is, if the two sides close a socket at the same time, then there is a situation where both sides send the fin message, there will be a closing state, indicating that both sides are shutting down the socket connection.
Close_wait: The meaning of this state is actually expressed in waiting to be closed. How do you understand it?when the other side close a socket to send fin message to yourself, you will undoubtedly respond to an ACK message to each other, then enter into the close_wait state. Next, the real thing you really need to consider is whether you still have the data sent to the other person, if not, then you can close the socket, send fin messages to each other, that is, close the connection. So what you need to accomplish in the close_wait state is waiting for you to close the connection. (Passive side)
Last_ack: This state is still relatively easy to understand, it is the passive close side after sending fin messages, and finally wait for each other's ACK message. When an ACK message is received, it is also possible to enter the closed available state. (Passive side)

CLOSED: Indicates a connection interruption.

The specific state diagram of TCP is available for reference:

Why TCP needs 3 handshake and 4 waves (reprint)

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.