TCP three handshake (establish connection) and four waves (release connection) detailed

Source: Internet
Author: User
Tags ack

The process of establishing a connection for TCP is drawn. Assume that host A is running a TCP client, and B runs a TCP service program. The TCP processes at the beginning of both ends are in the CLOSED (off) state. The boxes below the host are the states in which the TCP process is located, respectively. Note thata actively opens the connection, while B passively opens the connection.

b The TCP server process first creates the Transport control block TCB (Transmission control Blocks), ready to accept connection requests from the client process. The server process is then in listen (listening) state, waiting for the client's connection request. If so, make a response.

The TCP client process for a is also the first to create a transport control block TCB, then sends a connection request segment to B, at which point the synchronization bit in the header is SYN = 1, and an initial ordinal seq = x is selected. TCP Specifies that the SYN message segment (that is, the message segment of SYN = 1) cannot carry data, but consumes an ordinal number. at this point, the TCP client process enters the syn-sent (synchronous sent) state.

b After receiving the connection request message, if you agree to establish a connection, send a confirmation to a. Both the SYN and ACK bits should be set to 1 in the confirmation message segment, ACK = x + 1, and an initial ordinal seq = y is selected for itself. Please note that this segment is also not able to carry data, but it also consumes a serial number. At this point the TCP server process enters the SYN-RCVD (synchronously received) state.

After the TCP client process receives B's acknowledgement, it also needs to confirm to B. Confirm that the ACK of the message segment is 1, ack = y + 1, and that its ordinal seq = x + 1. The TCP standard specifies that the ACK segment can carry data. However, if you do not take the data without consuming the ordinal, in this case, the ordinal of the next data segment is still seq = x + 1. At this point, the TCP connection is established and a enters the established (established connection) state.

When B receives the confirmation of a, it also enters the established state.

The connection creation process given above is called a three-time handshake (three-way handshake).

Why does a have to send a confirmation? This is primarily to prevent a failed connection request message from suddenly being sent to B, resulting in an error.

Now look at the TCP four waves (release the connection)

Now both A and B are in the established state. A's application process first sends a connection release segment to its TCP and stops sending the data, actively shutting down the TCP connection.

A connect the end of the message segment header to the control position 1, its ordinal seq = U,u equals the last byte of the previously transmitted data plus 1. At this point a enters fin-wait-1 (terminating wait 1) status, waiting for the confirmation of B. Please note that the fin segment consumes a serial number even if it does not carry data.

b After receiving the connection release message segment is sent out confirmation, ack = U + 1, and this segment of its own ordinal is seq = V,v equals B before the last byte of the transmitted data sequence number plus 1. Then B enters the close-wait (off wait) state. The TCP server process should then notify the high-level application process, thus the connection from A to B is released, when the TCP connection is in the semi-closed (half-close) state, that a has no time to send, but b if the data is sent, a still needs to receive. That is, the connection from B to a is not closed, and this state may persist for some time.

A after receiving confirmation from B, enter fin-wait-2 (terminating wait 2) status, waiting for the connection release message segment from B.

If B has no data to send to a, its application process notifies TCP to release the connection. At this point, the connection release message segment sent by B must make fin = 1. It is assumed that the ordinal of B is w (some data may be sent in the semi-closed State B). b You must also repeat the last confirmed ACK number that you have already sent. = U + 1. At this point B enters the Last-ack (final confirmation) state and waits for a confirmation.

A confirmation must be issued upon receipt of a connection release confirmation from B. In the confirmation message section, the ACK is set to 1, the confirmation number ack = W + 1, and its own ordinal seq = U + 1 (according to the TCP standard, the previously sent fin message segment to consume a serial number). Then enter into the time-wait (time Wait) state. Note that the TCP connection is now not released and must go through the time- wait timer (time-wait timer) set to 2MSL after a before entering the closed state. The MSL is called the longest message segment life (Maximum Segment Lifetime), with a recommended time of 2 minutes or a smaller value. Therefore, after entering the time-wait state from A, it takes 4 minutes to enter the closed state before the next new connection can be established. When a revokes the corresponding transfer control block TCB, it ends the TCP connection.

B as soon as you receive a confirmation from a, you enter the closed state. Similarly, B ends this TCP connection after revoking the appropriate transfer control block TCB. We notice that B ends the TCP connection a bit earlier than a.

The above connection release process is a four-time wave .

Why does a have to wait for 2MSL time in time-wait state?

    1. In order to ensure that a sends the last ACK message segment to reach B.
    2. Prevents the connection request message segment that has failed to appear in this connection.

In addition to the time-waiting timer, TCP also has a keepalive timer (keepalive timer) to prevent the loss of resources caused by the client's host failure.

TCP three handshake (establish connection) and four waves (release connection) detailed

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.