Establishment and release of TCP connections (three handshake with four waves)

Source: Internet
Author: User
Tags ack

Establishment and release of TCP connections (three handshake with four waves)

TCP is a connection-oriented transport layer protocol that provides a reliable, full-duplex, byte-stream-oriented point-to-point service. The HTTP protocol is implemented based on the TCP protocol. (although as an application-level protocol, the HTTP protocol does not explicitly require that the TCP protocol be used as the transport layer protocol, but because of the HTTP protocol's reliability requirements, the default HTTP is based on the TCP protocol.) If HTTP is implemented using UDP's unreliable, best-effort transport-layer protocol, the TCP Protocol's traffic control, reliability assurance mechanisms, and so on, must all be put to the application layer, and the transport layer looks at the communication between application processes in comparison to the network layer. Instead of communicating between hosts on the network layer. Our common concepts of ports, sockets, and so forth, are born from this. (The port represents an application process on the host, and the socket is the combination of the IP address and the port number, which can be used to determine an application process within the network scope) the reliable transmission of TCP protocol is realized by sliding window method; Congestion control has slow start and congestion avoidance, fast retransmission and fast recovery, Red Random early detection methods. (These points of knowledge are not carefully summarized here, you can review the net textbook 23333)

In addition, the message format of the TCP protocol needs to be reviewed:

The first part of the TCP message segment is a fixed part and an option part, the fixed part is 20byte long, and the option part length is variable. (If the entire header length is not an integer multiple of 4byte, it needs to be filled with a fill bit) in the fixed header, the following items are closely related to this article:

SEQ (serial number): Each byte in the TCP connection stream has a number, and the value of this field refers to the ordinal of the first byte of the data part sent by this section.

ACK (Confirmation number): The number of the first byte of the data portion of the next segment of the packet that is expected to be received, numbered ack-1 and the previous bytes have been received.

SYN: When this field is 1 o'clock, this indicates a connection request or a connection acceptance message.

ACK: The confirmation number is valid only if this field is 1 o'clock.

FIN: Used to release a connection. When this field is 1 o'clock, it indicates that the sending end data for this segment has been sent, requiring the release of the transport connection.

The following is the focus of this article: TCP Transport Connection management.

Transport connections have three phases: connection establishment, data transfer, and connection release. Transport connection management is to establish the connection and the connection release process control, so that it can operate normally, to achieve these purposes: so that the two sides of the communication can be sure of the other side of the existence of the communication can be allowed to negotiate some parameters (maximum message segment length, maximum window size, etc.), the transport entity can be allocated (cache size, etc. The establishment of TCP connection adopts the client-server mode: The application process established by initiating the connection is called the customer, and the application process of passively waiting for the connection is called the server.

Connection Establishment phase:

First handshake: The client's application process is actively opened and a request message segment is sent to the client. In its first: Syn=1,seq=x.

Second handshake: The server application process is turned on passively. If you agree to the client's request, send back the confirmation message, whose header is: Syn=1,ack=1,ack=x+1,seq=y.

Third handshake: After the client receives the acknowledgement message, notifies the upper application process that the connection has been established and sends a confirmation message to the server, its header: Ack=1,ack=y+1. When the server receives a confirmation message from the client, it also notifies its upper application process that the connection has been established.

In this process, both sides of the communication state such as, where closed: off state, LISTEN: Listening status, syn-sent: Sync sent, SYN-RCVD: Sync received, estab-lished: Connection established

At this point, the TCP connection is established, and the client and server can play happily. The connection is maintained as long as no one on both sides of the communication makes a request to release the connection.

Connection Release phase:

First wave: After the end of the data transfer, the client's application process sends out the connection release message segment and stops sending the data, its header: Fin=1,seq=u.

Second wave: After the server receives the Connection Release message section, sends the confirmation message, its header: Ack=u+1,seq=v. At this point the connection enters a semi-closed state, and the client no longer sends data to the server. The server side continues to send.

Third Wave: If the server has no data to send to the client, its application process notifies the server to release the TCP connection. The last message sent by the server at this stage should be: fin=1,ack=1,seq=w,ack=u+1.

Fourth wave: After the client receives the connection release message segment, it must issue a confirmation: ack=1,seq=u+1,ack=w+1. After 2MSL (the longest reported relaying life), the TCP connection really ended, the two sides of the communication completed their farewell.

In this process, both sides of the communication state such as, where: estab-lished: Connection Establishment State, fin-wait-1: Terminating wait 1 state, fin-wait-2: Terminating wait 2 status, close-wait: Closing the wait state, Last-ack: final confirmation status , time-wait: Time wait status, CLOSED: Off state

To explain several issues uniformly:

1, in the process of shaking hands and waving, what is the meaning of the reciprocal ack and seq?

This is the communication between the two parties in the process of confirmation means to ensure that communication between the two sides of the correctness of communication. For example, the process of imitating TV radio calls in childhood: "Potato potatoes, I am sweet potatoes, can you hear?" "Sweet potato, I am a potato, I can hear." If the client's message request number is "potato", then the server side will return the confirmation number "potato +1" (sign potatoes have been received), is a means of confirmation of communication between the two sides.

2, in the process of ending the connection, why did the client still have to wait 2MSL to actually shut down the TCP connection after receiving the server-side connection release message segment?

Here are two reasons: the first is to ensure that the server side receives the client's last acknowledgment message. If this message is lost, the server does not receive a confirmation message, the connection release message will be timed out retransmission, and when the client connection is closed, unable to respond, causing the server to continue to re-transmit the connection to release the message, and can not enter the state of the shutdown status. While waiting for 2MSL, you can ensure that the server has received a final confirmation, if the server is not received, then within 2MSL the client will receive the server-side retransmission message, the client will re-transmit the acknowledgement message, and reset the timer.

The second is: there is a "Failed connection request message segment", you need to avoid this message end out of the current connection, causing an exception.

This "Failed connection request message segment" is formed: if the client sent a connection request message, but the server did not receive, so the client to time-out retransmission, once again sent the connection request message, and successfully established the connection. However, the first time the connection request message is not lost, but only in a network node for a long time, and then, the original packet sent to the server side, the server side will be mistaken for the client to make a new request, causing the exception.

3. What happens if both sides of the communication request a connection at the same time or request a free connection?

Although this situation is very unlikely, but it does exist, TCP also deliberately designed the mechanism, so that in this case the two sides to establish only one connection. When both parties request the connection, the two parties simultaneously make the request connection message and enter the Syn-sent state, and when the request connection message is received, the request connection message is sent again, the confirmation number is the syn+1 of the other, and enters the SYN-RCVD state. The estab-lished status will be entered after receiving a request message with the confirmation number issued by the other party for the second time. The two parties simultaneously request to release the connection is same, the two sides simultaneously sends the connection release message, and enters the fin-wait-1 state, after receives the other's message, sends the confirmation message, and enters the closing state, after receiving the other's confirmation message, enters the time-wait state, After waiting for 2MSL, close the connection. It is important to note that this time, although it is not necessary to send a confirmation message again and confirm that the other party received, the two sides still need to wait 2MSL and then close the connection, is to prevent the "Invalid connection request message segment" effect. The process diagram is as follows:

Establishment and release of TCP connections (three handshake with four waves)

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.