TCP Three-time handshake, four wave waving and state transition diagram

Source: Internet
Author: User

The three-time handshake for TCP/IP communication is as follows:

TCP is the host-to-host layer of Transmission Control Protocol, providing reliable connection services:

The bit code , the TCP flag bit, has 6 types of markings:SYN(synchronous),ACK(acknowledgement),PSH( Push transfer), FIN(finish end),RST(reset reset),URG(Urgent emergency),Sequencenumber (sequential), Acknowledge number (Confirmation number).

Three-time handshake:

First handshake: The client sends the SYN packet (syn=x) packet to the server and enters the Syn_send state, waiting for the server to confirm;

Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=X+1), and also send itself a SYN packet (syn=y), that is, the Syn+ack packet, when the server enters the SYN_RECV state;

Third handshake: The client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=Y+1) to the server, the packet is sent, the client and the server enter the established state, and the handshake is completed three times.

The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is complete. Ideally, once a TCP connection is established, the TCP connection is maintained until either side of the communication actively closes the connection.

Four-time handshake:

First wave: The active shutdown sends a FIN, which is used to close the active side to the passive closed side of the data transfer, that is, the active shutdown to tell the passive shut-off party: I will no longer send you the data (of course, the data sent before the FIN packet, if not received the corresponding ACK acknowledgement message, The active shutdown will still re-send the data), but the active shut-off party can also accept the data at this time .

Second wave: The passive closing party receives the fin packet, sends an ACK to the other, confirms that the serial number is received ordinal +1 (same as SYN, one fin occupies a serial number).
Third wave: The passive shut-off side sends a fin, used to close the passive shut-off side to the active shutdown of the data transfer, that is, to tell the active shut-off party, my data is sent out, will not send you data.
Wave for the fourth time: the active closing party receives fin, sends an ACK to the passive closing party, confirms the serial number to receive the serial number +1, to this point, completes four times the wave.

TCP state transition diagram

CLOSED: This is nothing to say, it means the initial state.

LISTEN (server): This is also very easy to understand a state, that the server side of a socket is listening state , can accept the connection.

YN_RCVD (server): This status indicates that the SYN message was 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 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.

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 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 .

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.

  Note: MSL (maximum segment lifetime) indicates the longest lifetime of a TCP message on the Internet, and each specific TCP implementation must select a certain MSL value. RFC 1122 recommends 2 minutes, but BSD legacy implementations take 30 seconds. time_wait State maximum hold time is 2 * MSL, which is 1-4 minutes.

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 will be two sides simultaneously send fin message situation, will appear closing state, Indicates 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.

Thinking:

To be Continued ...

Reference:

Time_wait status in TCP connections-Sunnydogzhou's column-blog channel-Csdn.net http://blog.csdn.net/sunnydogzhou/article/details/6572071

TCP connection status detailed and time_wait too many solutions _ Xiao Qiang _ Sina Blog http://blog.sina.com.cn/s/blog_8e5d24890102w9yi.html

TCP Three-time handshake connection four handshake disconnects and Dos attacks-nowornever-Blog channel-Csdn.net http://blog.csdn.net/fw0124/article/details/7452695

TCP Three-time handshake, four wave waving and 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.