The three-way handshake of TCP and the specific state transformation of each step of the four-way handshake process

Source: Internet
Author: User

1. TCP transport connection management

TCP is a connection-oriented protocol. Transport connections are used to send TCP packets. The establishment and release of TCP transport connections are essential for every connection-oriented communication. transport connections have three phases: Connection establishment, data transmission, and connection release. The management of transportation connections is to make the establishment and release of transportation connections normal.

When establishing a TCP connection, you must solve the following three problems:

(1) so that each party can know the existence of the other party.

(2) allow both parties to coordinate some parameters (such as the maximum window value, whether to use window expansion options, TIMESTAMP options, and service quality ).

(3) It can transport physical resources (such as the cache size and projects in the connection table) for allocation.

TCP is established on the client server. The application process that initiates the connection is called the client, and the application process that passively waits for the connection to be established is called the server ).

2. Establish a TCP connection

650) This. width = 650; "src =" http://s4.51cto.com/wyfs02/M01/80/1B/wKioL1c4NM7zqpMuAACDkGzxObM735.jpg "Title =" l1.jpg "alt =" wkiol1c4nm7zqpmuaacdkgzxobm735.jpg "/>

Shows the process of establishing a connection over TCP. Assume that host a runs the tcp client program, while host B runs the TCP server program. TCP at both ends is initially in the closed state. A actively opens the connection, and B passively opens the connection.

The TCP server process of B first creates a transmission control block TCP, ready to accept the connection request of the client process. Then the server process is in the listen status, waiting for the client's connection request. If yes, a response is made.

The TCP server process of A is also the first to create the transmission control module TCP, and then send a connection request packet segment to B, which is the same part of the header SYN = 1, and select an initial sequence number seq = x. TCP stipulates that the SYN packet segment (that is, the SYN = 1 packet segment) cannot carry data, but consumes a serial number. The tcp client process enters the SYN-SENT (synchronous sent) state.

After B receives the connection request message segment, if B agrees to establish a connection, it will send confirmation to. In the validation packet segment, set both the SYN bit and ACK bit to 1, check that the number is ACK = x + 1, and select an initial sequence number seq = Y for yourself. This packet segment cannot carry data, but it also consumes a serial number. The TCP server process enters the SYN-RCVD (synchronization sent) state.

After receiving confirmation from B, the tcp client process should also confirm with B. Set ack to 1, ack to Y + 1, and SEQ to x + 1. According to TCP standards, ACK packets can carry data. However, if no data is carried, no sequence number is consumed. In this case, the sequence number of the next data packet segment is still seq = x + 1. At this time, the TCP connection has been established, and a enters the established (established connection) status.

After receiving confirmation from a, B enters the established (established connection) status.

The above connection creation process is called three-way handshake.

Why does a send a confirmation?

The main purpose is to prevent the failure of the connection request packet segment from being suddenly transmitted to B, thus generating errors.


Ii. TCP connection release

650) This. width = 650; "src =" http://s5.51cto.com/wyfs02/M01/80/1B/wKioL1c4PjmjaG2qAACQqFm6byI323.jpg "Title =" l2.jpg "alt =" wkiol1c4pjmjar2qaacqqfm6byi323.jpg "/>

The release process of TCP connections is complex, which can be explained by the status changes of both parties.

After the data transmission is completed, both parties can release the connection. Both A and B are in the established status. The application process of a first releases the packet segment from its TCP connection, stops sending data, and closes the TCP connection. A sets the end control bit fin of the first part of the connection release packet segment to 1, and the sequence number of the phase sequence is seq = U, which is equal to the sequence number of the last byte of the previously transmitted data plus 1. This is the status of a entering fin-wait-1 (Termination wait 1), waiting for B's confirmation. TCP stipulates that even if the fin segment does not carry data, it consumes a sequence number.

B sends a confirmation message after receiving the connection release message segment. The confirmation number is ACK = u + 1, and the serial number of the packet segment is V, which is equal to the serial number of the last byte of the data transmitted before B plus 1. then B enters the close-Wait (close Wait) state. At this time, the TCP server process should notify the high-level application process, so the connection from A to B is released. At this time, the TCP connection is in the semi-closed state, that is, a has no data to send, however, if B sends data, a still needs to receive it. That is to say, the connection from B to a is not closed, and the status may last for a period of time.

After receiving the confirmation from B, A enters the fin-wait-2 (Stop wait 2) Status and waits for B to release the packet segment.

If B has no data to send to a, its application process notifies TCP to release the connection. This is the connection sent by B. The packet segment must be fin = 1. Assume that the serial number of B is W (B may have sent some data in the semi-closed state ). B must also repeat the last sent confirmation number ACK = u + 1. This is where B enters the LAST-ACK (final confirmation) status, waiting for a's confirmation.

A must confirm after receiving the connection release packet segment from B. In the validation packet segment, set ack to 1 and check ACK = W + 1, while its serial number is seq = u + 1 (Fin consumes a serial number ). Then enter the time-Wait Status. The TCP connection has not been released yet. A enters the closed State only after 2msl of the timer is set. The time MSL is called the longest message segment lifetime, and rfc793 is recommended to be set to 2 minutes. TCP allows different implementations to use smaller MSL according to specific circumstances. When a revokes the corresponding transmission control block TCP, the TCP connection ends.

Why must a wait for 2msl in time-Wait Status?

(1) to ensure that the last ACK packet segment sent by a can reach B. This ACK packet segment may be lost, so that B in the LAST-ACK State cannot receive confirmation of the sent FIN + ACK packet segment. B will retransmit the FIN + ACK packet segment timeout. A can receive the FIN + ACK packet segment of the retransmission within 2msl. Then a re-transmits the message and restarts the 2msl timer. At last, both A and B enter the closed State normally. If a is in the Time-Wait Status and does not wait for a while, but immediately releases the connection after the ACK packet segment is sent, then B cannot receive the FIN + ACK packet segment sent by B, therefore, no validation packet is sent. In this way, B cannot enter the closed state according to the normal steps.

(2) Prevent invalid connection request packets from appearing in this connection. After sending the last ACK packet segment, after 2msl, A can make all the packet segments generated during the connection duration disappear from the network. In this way, the old connection request packet segment will not appear in the next new connection.



This article is from the "tassel" blog, please be sure to keep this source http://ab6107.blog.51cto.com/10538332/1773751

The three-way handshake of TCP and the specific state transformation of each step of the four-way handshake process

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.