The process of establishing a connection (3-way handshake) and disconnecting (4-way wave) of the TCP protocol

The TCP protocol (Transmission Control Protocol, Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol, defined by the IETF's RF793.

Connection-oriented means that two applications using TCP (usually a client and a server) must establish a TCP connection before exchanging data with each other. This process is very similar to making a phone call, first dialing and ringing, waiting for the other party to pick up the phone and say "hello", and then explain who it is. The data transmission of both parties can be carried out through this one connection. After completing the data exchange, both parties must disconnect this connection to free system resources. This connection is one-to-one. This article focuses on the process of establishing a connection (3-way handshake) and disconnecting (4-way wave) of the TCP protocol by analogy to the example of making a phone call in life.

First you need to understand a few fields:

(1) seq sequence number : occupies 32 bits, used to identify the byte stream sent from the TCP source to the destination, and the initiator will mark this when sending data. Its initial sequence number is random, and the relative sequence number/acknowledgment sequence number is associated with the initial sequence number of the TCP session. This sequence number is used to track the amount of data sent by that end. Each packet contains a sequence number, and at the receiving end, the confirmation sequence number is used to notify the sender that the data has been successfully received .

(2) Confirmation serial number : ack serial number, occupying 32 bits, only when the ACK flag bit is 1 ( do not confuse the confirmation serial number ack with the ACK in the flag bit ), the confirmation serial number is valid. Confirmer ack = Initiator s eq +1 , both ends are paired  .   

(3) Bit code is the TCP flag bit , there are 6 kinds, the specific meaning is as follows:

    SYN (synchronous connection establishment)  initiates a new connection

    ACK ( acknowledgment  means response, confirmation)  confirms that the serial number is valid

    PSH (push means that there is DATA data transmission)  The receiver should hand over this message to the application layer as soon as possible

    FIN (finish closes the connection)  releases a connection

    RST (reset means connection reset)

    URG (urgent  pointer urgent pointer field value is valid)

1. Understanding the three-way handshake process

The so-called three-way handshake (Three-Way Handshake) is to establish a TCP connection, which means that when establishing a TCP connection, the client and the server need to send a total of 3 packets to confirm the establishment of the connection. In socket programming, this process is triggered by the client executing connect .

The following picture is an example of a phone call in life:


Figure 1. The connection established during the phone call

Three-way handshake:

(1) The first handshake: The client client sets the flag SYN to 1, randomly generates a sequence number value (seq = J), sends a SYN packet to the server Server, and the client enters the SYN_SEND state, waiting for the server to confirm.

( 2 ) The second handshake : After the server receives the data packet, the flag bit SYN = 1 knows that the client requests to establish a connection, and the server sets the flag bits SYN and ACK to 1, ack = J+1, and randomly generates a value seq = K, and sends the data packet to the Client to confirm the connection request, and the Server enters the SYN_RCVD state.      

( 3 ) The third handshake: After the client receives the confirmation, it checks whether the ack is J+1 and whether the ACK is 1. If it is correct, the flag bit ACK is set to 1, ack = K + 1, and the packet is sent To the server, the server checks whether the ack is K+1 and whether the ACK is 1. If it is correct, the connection is established successfully. The client and the server enter the ESTABLISHED state, complete the three-way handshake, and then the client and the server can start to transmit data.    

The packet transmitted during the handshake process does not contain data. After the three-way handshake is completed, the client and the server officially begin to transmit data. Ideally, once a TCP connection is established , the TCP connection will be maintained until either party actively closes the connection.

Confirmation number: its value is equal to the sender's sending sequence number + 1 (that is, the next sequence number that the receiver expects to receive).

Figure 2. Three-way handshake in TCP (connection establishment)

Question extension: Why can't I connect with two handshakes?

Answer: The 3-way handshake accomplishes two important functions. It not only requires both parties to prepare for sending data (both parties know that each other is ready), but also allows both parties to negotiate the initial sequence number, which is used in the handshake process. is sent and confirmed.
    Now change the three-way handshake to only two handshakes, and deadlock is possible. As an example, consider the communication between computers S and C. Suppose that C sends a connection request packet to S, and S receives this packet and sends an acknowledgment response packet. According to the agreement of the two-way handshake, S believes that the connection has been successfully established and can start sending data packets. However, in the case that the response packet of S is lost in transmission, C will not know whether S is ready or not, and does not know what sequence number S has established, and C even doubts whether S has received its own connection request packet. In this case, C considers that the connection has not been established successfully, will ignore any data packets sent by S, and only wait for the connection confirmation response packet. However, after the sent packet times out, S repeats sending the same packet. This creates a deadlock.

2. Comprehension of the process of waving hands four times

The so-called Four-Way Wavehand terminates the TCP connection, which means that when a TCP connection is disconnected, the client and the server need to send a total of 4 packets to confirm the disconnection of the connection. In socket programming, this process is triggered by either the client or the server executing close . The disconnected end can be the client end or the server end.

Since the TCP connection is full-duplex, each direction must be closed separately. The principle is that when one party completes the data transmission task, it sends a FIN to terminate the connection in this direction. Receiving a FIN only means that There is no data flow in this direction, that is, no more data will be received, but data can still be sent on this TCP connection until FIN is also sent in this direction. The side that shuts down first will perform an active shutdown, while the other side will perform a passive shutdown .

An example of a disconnected phone connection in life is shown in the image below:


Figure 3. An example of disconnecting a phone by analogy with four waves of hands

Four waves:

Figure 4. Schematic diagram of waving hands four times

(1) Wave for the first time: The client sends a FIN to close the data transfer from the client to the server, and the client enters the FIN_WAIT_1 state.

(2) The second wave: After the server receives the FIN, it sends an ACK to the client, confirming that the serial number is the received serial number + 1 (same as SYN, one FIN occupies one serial number), and the server enters the CLOSE_WAIT state.

( 3) The third wave: The server sends a FIN to close the data transmission from the server to the client, and the server enters the LAST_ACK state.

( 4 ) The fourth wave: After the Client receives the FIN, the Client enters the TIME_WAIT state, and then sends an ACK to the Server, confirming that the serial number is the received serial number + 1, and the Server enters the CLOSED state, completing the four waveds.


Expansion of the problem: Why is it a three-way handshake to establish a connection, but a four-way wave to close the connection?

Answer: This is because the server in the LISTEN state, after receiving the SYN message for the connection establishment request, sends the ACK and SYN in one message to the client. When closing the connection, when receiving the FIN message from the other party, it only means that the other party no longer sends data but can still receive data, and not all data is sent to the other party, so the party can immediately close or send some data. After the data is sent to the other party, the FIN message is sent to the other party to express the agreement to close the connection now. Therefore, the own ACK and FIN are generally sent separately.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734880&siteId=291194637