Three handshake of TCP and detailed description of TCP state transition diagram

Source: Internet
Author: User

Let's talk about the TCP three handshake and the TCP state transition diagram today. First, a three-time handshake flow chart is as follows:

2.4-3, three-way AC-Grip Packet Connection mode A: Packet Initiation When the client wants to initiate a connection to the server, it is necessary to send a packet that requires a connection, at which point the user must randomly take a port greater than 1024 to serve as a channel for program communication. Then in the TCP header, you must have a SYN active line (syn=1) and write the serial number to the server side (Sequence # = 10001). B: Packet receiving and confirmation packet sending When the server receives this package, and is sure to accept the package, it will start to produce a packet with Syn=1, Ack=1, where the acknowledge number is to be confirmed to the client side, so the number will be more than (A step) inside the Sequence number 1 ( ACK = 10001+1 = 10002), then our server must also confirm that the client can actually receive our package, so it will send a sequence (seq=20001) to the user and start waiting for the response from the clients. C: Send Confirmation Package When the client receives the ACK number sent by the server (10002), it can confirm that the previously sent packet is received, then agrees to establish a connection with the server, and sends a confirmation packet (ack=1) to the server again, i.e. acknowledge = 20001+1 = 2000 2. D: Get a final confirmation If everything goes well, the client and server side are connected after receiving a package with ack=1 and ack=20002 serial number on the server side. Let's introduce the TCP state transition diagram as follows:

Note: There is no explicit correspondence between active, passive and server, client. This figure n many people know that it helps to troubleshoot and locate network or system failures, but how do you keep this picture in mind? Then you must have a deep understanding of each state of the graph and the process of conversion, and not just stay in the smattering. Below is a detailed analysis of the 11 states of this graph in order to enhance the memory!

CLOSED: This is nothing to say, it means the initial state.
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_RCVD: This status means that the SYN message is received, under normal circumstances, this state is the server side of the socket in the establishment of a TCP connection during the three handshake session process of an intermediate state, very short, basically with netstat you are very difficult to see this state, Unless you specifically write a client-side test program, deliberately not send the last ACK message during the three TCP handshake. Therefore, when the ACK message is received from the client, it goes into the established state.
Syn_sent: This state is echoed with the syn_rcvd thinking back, when the client socket performs a connect connection, it sends the SYN message first, so it then enters the syn_sent state and waits 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.
Established: This is easy to understand, indicating that the connection has been established.
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.
Fin_wait_2: Above has explained in detail this state, actually fin_wait_2 the socket in the state, indicates the half connection, also namely has the party request close connection, but also tells the other side, I temporarily also some data need to transmit to you, later closes the connection again.
Time_wait: Said to receive the other side of the fin message, and sent out an ACK message, just wait for 2MSL to return to the closed usable 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.
CLOSING: This kind of state is special, the actual situation should be very rare, belong 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.
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.
Finally there are 2 questions answered, my own analysis of the conclusion (not necessarily guaranteed 100% correct)
1, why to establish a connection agreement is three handshake, and close the connection is four handshake it?
This is because the socket in the listen state of the server is sent in a message after it receives a request for the connection of the SYN message, and it can put the ACK and SYN (ACK response, and SYN synchronous). However, when the connection is closed, when receiving the other's fin message notification, it simply means that no data is sent to you, but not all of your data are sent to the other side, so you may not immediately close the socket, that is, you may also need to send some data to the other side, Send the FIN message to the other side to show that you agree that you can now close the connection, so it is here that the ACK message and fin messages are sent separately in most cases.
2, why time_wait state also need to wait 2MSL to return to closed status?
This is because: although both sides agree to close the connection, and the handshake of the 4 messages are also coordinated and sent, can be directly back to the closed state (like from the Syn_send state to establish state); but because we have to assume that the network is unreliable, You cannot guarantee that the last ACK message you send will be received by the other party, so the socket in the Last_ack state may be re-sending the fin message because the timeout does not receive an ACK message, so this time_wait state is used to resend the possible missing ACK message.

Also open

It is possible for two applications to perform active open at the same time, although the likelihood of occurrence is low. Each end sends a SYN and passes it to each other, and each end uses the port known to the end as the local port. For example:

An application in host a uses 7777 as the local port and connects to Host B 8888 Port to open it actively.

Host B An application uses 8888 as the local port and connects to host a 7777 port to do the active open.

TCP protocol When this happens, only one connection is opened. The process of establishing this connection requires 4 data exchanges, while a typical connection establishment requires only 3 exchanges (that is, 3 handshakes), but most of the Berkeley versions of TCP/IP implementations do not support simultaneous opening.

Both SYN_RCVD and Syn_send are intermediate states that are converted to established, with the goal being to convert both ends to the established state. <

Three handshake of TCP and detailed description of TCP state transition diagram

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.