Three-way handshake, four-way handshake

After sorting out textbooks, Baidu Encyclopedia, and other blogs, the summary is as follows:

 

The first handshake: When the connection is established, the client sends a syn packet (syn=j) to the server , and enters the SYN_SENT state, waiting for the server to confirm; SYN: Synchronize Sequence Numbers.

The second handshake : the server receives the syn packet, and must confirm the client's SYN ( ack =j+1), and also sends a SYN packet (syn=k), that is, the SYN+ACK packet, at this time the server enters the SYN_RECV state;

The third handshake: the client receives the SYN+ACK packet from the server , and sends the confirmation packet ACK ( ack = k+1) to the server. After the packet is sent, the client and the server enter the ESTABLISHED (TCP connection is successful) state and complete. Three handshake.

After completing the three-way handshake, the client and the server begin to transmit data . In the above process, there are some important concepts:

unconnected queue

In the three-way handshake protocol , the server maintains an unconnected queue that opens an entry for each client 's SYN packet ( syn = j), which indicates that the server has received the SYN packet and sends an acknowledgment to the client, waiting for Customer's confirmation package. The connection identified by these entries is in the SYN_RECV state on the server . When the server receives the confirmation packet from the client, the entry is deleted and the server enters the ESTABLISHED state.

 

Close TCP connection: improved three-way handshake

For an established connection, TCP uses a modified three-way handshake to release the connection (using a segment with an additional FIN tag). The steps for TCP to close a connection are as follows:

In the first step, when the application program of host A informs the TCP that the data has been sent, TCP sends a message segment with the additional mark of FIN to host B (FIN means finish in English).

In the second step, after the host B receives the FIN segment, it does not immediately reply to the host A with the FIN segment, but first sends a confirmation sequence number ACK to the host A, and at the same time informs its corresponding application: the other party requests to close Connection (the purpose of sending ACK first is to prevent the other party from retransmitting the FIN segment during this period).

In the third step, the application of host B tells TCP: I want to completely close the connection, and TCP sends a FIN segment to host A.

In the fourth step, after host A receives the FIN segment, it sends an ACK to host B to indicate that the connection is completely released.

Why use a three-way handshake instead of two?

 

 

 

 

 

 

Keep Alive Timer:

Imagine a situation where the client has actively established a TCP connection with the server , but then the client's host suddenly fails .

Usually set to 2 hours . If the data from the client is not received within 2 hours, the server sends a probe segment, and then sends it every 75 minutes. If there is still no response from the client after sending 10 probe segments in a row , the server considers the client to be faulty and then closes the connection.

 

 

 

 

Why is there a three-way handshake when connecting and a four-way handshake when closing?

Answer: Because when the server receives the SYN connection request message from the client, it can directly send the SYN+ACK message. The ACK message is used for response, and the SYN message is used for synchronization. However, when closing the connection, when the server receives the FIN message, it is likely not to close the SOCKET immediately, so it can only reply an ACK message to tell the client, "I received the FIN message you sent". I can only send FIN messages until all the messages on my server have been sent, so I cannot send them together. Therefore, a four-step handshake is required.

 

Why does the TIME_WAIT state need to go through 2MSL (maximum segment lifetime) to return to the CLOSE state?

Answer: Although it is reasonable to say that all four packets have been sent, we can directly enter the CLOSE state, but we must pretend that the network is unreliable, and the last ACK may be lost. So the TIME_WAIT state is used to resend ACK messages that may be lost.

Copyright statement: Respect originality, please indicate the source for reprint http://blog.csdn.net/qq_18425655 https://blog.csdn.net/qq_18425655/article/details/52163228

Guess you like

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