TCP/IP three-time handshake 4 disconnects

Source: Internet
Author: User
Tags ack

The TCP/IP protocol has a 3-time handshake of 4 disconnects:


(Note: SYN is used to establish the connection sck used to confirm that the connection rst is used to reset the connection error)

(Listen listening status syn_sent waiting for a matching connection request after sending a connection request syn_received waiting for confirmation of connection request after receiving and sending a connection request established complete connection, can transmit data Sequence number order Acknowledge Number Confirmation number FIN (finish end) URG (urgent emergency) PSH (push transfer)



Three-time handshake:



Client: Sends a connection request to the server side-----syn=1 ack=0 randomly generates a sequence number of messages to the server side, the status is Syn_send, waiting for the server to answer. At this point the server side is the listen state.

Server: Accepts a request to send a response that asks the client if the data connection is ready----syn=1 ack=1, receives syn=1, sends Ayn=1 ack=1 that syn+ack the package, and the server side is SYN_RECV state.

Client: Confirm that the connection client receives the server's Syn+ack packet, sends the confirmation connection message to the server side----syn=1+1, completes the connection client and server to enter established state simultaneously, completes three times the handshake, For data transfer



Four-time disconnection:




1, the client sends a fin to shut down the client-to-server data transfer, when the client enters the TIME_WAIT1 state

2, the server received this fin, it sent back an ACK, the confirmation number for the received sequence number plus 1, and SYN, a fin will occupy a sequence number , when the service enters the close_wait state, the client side into the Time_wait2

3, when the server side also does not have the data to be transferred, the server shuts down the connection with the client, sends a fin to client A, the server enters the Last_ack

4, the client sends back ACK message confirmation, and the confirmation number is set to receive serial number plus 1, the client enters Time_wait, waits for 2msl--120s, the server is terminated to enter closed



--------------------------------the above part by virtue of their own understanding of pure hand-------------------------


-------------------------------------the following part of the Red Hat------------------------------------


Three-time handshake


TCP is the host-to-host layer of the Transmission Control Protocol, providing a reliable connection service, using three-time handshake confirmation to establish a connection:


The bit code is the TCP flag bit, and there are 6 types of markings:


SYN (synchronous set up online)


ACK (Acknowledgement confirmation)


PSH (push transfer)


FIN (end of finish)


RST (Reset reset)


URG (Urgent emergency)


Sequence number (sequential numbers)


Acknowledge Number (confirmation)



Client TCP State Migration:

Closed->syn_sent->established->fin_wait_1->fin_wait_2->time_wait->closed

Server TCP State Migration:

Closed->listen->syn received->established->close_wait->last_ack->closed



The meanings of each state are as follows:

LISTEN-listens for connection requests from remote TCP ports;

Syn-sent-waits for a matching connection request after the connection request is sent;

Syn-received-Waits for confirmation of the connection request after receiving and sending a connection request;

established-represents an open connection, the data can be transmitted to the user;

Fin-wait-1-a connection interruption request waiting for a remote TCP, or confirmation of a previous connection interruption request;

Fin-wait-2-Waits for a connection interrupt request from a remote TCP;

Close-wait-waits for a connection interruption request from a local user;

CLOSING-Wait for the remote TCP acknowledgement of the connection interruption;

Last-ack-Acknowledgement of the connection interrupt request that was originally sent to the remote TCP;

Time-wait-wait enough time to ensure that the remote TCP receives the acknowledgement of the connection interrupt request;

CLOSED-no connection status;



In the TCP/IP protocol, the TCP protocol provides a reliable connection service with a three-time handshake to establish a connection, as shown in 1.


(1) First handshake: When a connection is established, client A sends a SYN packet (SYN=J) to Server B and enters the syn_send state, waiting for Server B to confirm.


(2) Second handshake: Server B receives the SYN packet, must confirm the SYN (ACK=J+1) of customer A, and also sends a SYN packet (syn=k), that is, the Syn+ack package, at which point Server B enters the SYN_RECV state.


(3) Third handshake: Client A receives Server B's Syn+ack package, sends acknowledgment packet ACK (ack=k+1) to Server B, the packet is sent, client A and Server B enter the established state, complete three handshake.


Three handshake is completed and the client and server begin to transfer data.



Four-time disconnection:



Because TCP connections are full-duplex, each direction must be closed separately. The principle is that when a party completes its data sending task, it can send a fin to terminate the connection in this direction. Receiving a fin only means there is no data flow in this direction, and a TCP connection can still send data after receiving a fin. The first party to close will perform an active shutdown, while the other side performs a passive shutdown.


The removal of the CP connection requires four packets to be sent, so it is called a four-time wave (Four-way handshake). Either the client or the server can initiate a wave gesture, and in socket programming, any party performs a close () operation to generate a wave.


(1) Client A sends a fin to turn off the data transfer from client A to Server B.


(2) Server B receives this fin, which sends back an ACK, confirming that the serial number is received plus 1. As with Syn, a fin will occupy a sequence number.


(3) Server B closes the connection to client A and sends a fin to client a.


(4) Client A sends back ACK message confirmation, and sets the confirmation sequence number to receive the serial number plus 1.




Deep understanding of the release of TCP connections:



Because TCP connections are full-duplex, each direction must be closed separately. The principle is that when a party completes its data sending task, it can send a fin to terminate the connection in this direction. Receiving a fin only means there is no data flow in this direction, and a TCP connection can still send data after receiving a fin. The first party to close will perform an active shutdown, while the other side performs a passive shutdown.

The connection to the TCP protocol is a full-duplex connection, and a TCP connection has a bidirectional read-write channel.

In short, "First shut down, then write", there is a total of four stages. Take a client-initiated shutdown connection as an example:

1. Server Read Channel shutdown

2. Client Write channel shutdown

3. Client Read channel shutdown

4. Server Write channel shutdown

The shutdown behavior is to send a fin (finish) data segment to the sender after the initiator data has been sent. Until you receive the fin sent by the other party, and the other party receives the acknowledgement ACK, the data communication between the two sides is completely terminated, and each receive in the process needs to return the acknowledgment segment ack.

Detailed process:

After the first phase of the client sends the data, a fin data segment is sent to the server with the serial number I;

1. After receiving fin (i), the server returns the acknowledgment segment ACK, the serial number is i+1, and the server read channel is closed;

2. After the client receives an ACK (I+1), it shuts down the client write channel;

(At this point, the client can still read the server's data through the read channel, the server can still write data through the Write channel)

After the second stage the server sends the data, sends a FIN data segment to the client, the serial number is J;

3. After the client receives fin (j), it returns the acknowledgment segment ACK, the serial number is j+1, and the client read channel is closed;

4. After the server receives an ACK (J+1), shut down the server write channel.

This is a standard TCP shutdown of two stages, and both the server and client can initiate shutdown, completely symmetric.

The fin ID is set when the last piece of data is sent, in the standard example, the server is still sending data, so wait until the send is finished, set fin (this can be referred to as the TCP connection is semi-closed, because the data can still be transferred from the passive shutdown in the direction of active shutdown). If there is no data to send when the server receives fin (i), it is possible to set the Fin (j) identifier when the ACK (I+1) is returned, which is equivalent to merging the second and third steps. Read "Linux network Programming" to close the TCP Connections section and make the following notes:


This article is from the "Summer Flowers," blog, please be sure to keep this source http://xiamin.blog.51cto.com/11258876/1834443

TCP/IP three-time handshake 4 disconnects

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.