Why is TCP three-way handshake four times hui'shou

Why is TCP three-way handshake four times hui'shou

Operation and Maintenance YouthO&M Youth

Introduction to TCP

TCP provides a connection-oriented, reliable byte stream service. Among them, connection-oriented means that two applications that use TCP (usually a client and a server) must establish a TCP connection before exchanging data with each other. In a TCP connection, only two parties communicate with each other; and the byte stream service means that two applications exchange a byte stream composed of 8bit bytes through a TCP link, and TCP does not insert a record identifier in the byte stream.
Why is TCP three-way handshake four times hui'shou

Three handshake

TCP establishes a connection relationship through a three-way handshake, and then performs data exchange after the connection relationship is established.

How does the three-way handshake work?

  • The first handshake: The client sends a request message to the server, SYN=1, ACK=0, a value seq=x is randomly generated, and the client enters the SYN_SENT state, waiting for the server to confirm.
  • The second handshake: After the server receives the request message, if it agrees to establish a connection, it sends a connection confirmation message to the client, SYN=1, ACK=1, ack=x+1, and randomly generates a value seq=y, Server enters the SYN_RCVD state.

  • The third handshake: After the client receives the confirmation, it checks whether ack is x+1 and ACK is 1, and if it is correct, it sends an acknowledgment message to the server, ACK=1, ack=y+1, seq=x+1, Server checks whether ack is x+1 and ACK is 1. If it is correct, the connection is established successfully. Client and Server enter the ESTABLISHED state, complete the three-way handshake, and then start data transmission between Client and Server.

Why is TCP three-way handshake four times hui'shou

Why do you need to shake hands three times?

Imagine that A sends a request to connect for the first time, but it is stuck on a certain node in the network, and A retransmits after a timeout, and then everything is normal this time, and A and B are happily transmitting data. After the connection is released, the lost connection request suddenly arrives at B. If it is a two-way handshake, B sends an acknowledgment, and they have established a connection. In fact, A doesn't care about this confirmation, because I don't want to send data at all. But B foolishly thought that there was data to come, and waited so hard, which resulted in a waste of resources. The third handshake is to prevent invalid connection requests from reaching the server and let the server open the connection by mistake.

Understand the correct posture for the three-way handshake

(1) In the first handshake, after A sends a message to B, B receives the message. B can confirm A's sending ability and B's receiving ability.
(2) In the second handshake, B sends a message to A, and A receives the message. A can confirm A's sending ability and receiving ability, and A can also confirm B's receiving ability and sending ability.
(3) In the third handshake, A sends a message to B, and B receives the message. B can confirm A's receiving ability and B's sending ability.

A more down-to-earth explanation is: A calls B to
shake hands for the first time: Hello, I am A, can you hear me
second handshake: Yes, I am B, can you hear me?
The third handshake: I heard that, we can start chatting. The
three handshake is actually to check whether the sending and receiving capabilities of both parties are normal, what do you think?

Wave four times

 TCP通过四次挥手进行连接关闭。

How to wave four times?

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

  • The second wave: After receiving the FIN, the server sends an acknowledgement message to the client, ACK=1, seq=v, ack=u+1 (same as SYN, one FIN occupies one sequence number), and the server enters the CLOSE_WAIT state. At this time, the TCP link is in a half-closed state, that is, the client has no data to send, but if the server sends data, the client still has to receive it.

  • The third wave: Server sends a FIN to close the data transfer from Server to Client, and Server enters the LAST_ACK state.

  • 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 waits for 2.

  • Enter the CLOSED state after MSL (Maximum Message Survival Time). The Server enters the CLOSED state after receiving the ACK from the Client and completes four waves

Why is TCP three-way handshake four times hui'shou

Why wave four times instead of two, three times?

(1) Due to TCP's full-duplex communication, both parties can act as data senders. If A wants to close the connection, it must wait for the data to be sent before sending FIN to B. (At this time, A is in a half-closed state)

(2) B sends confirmation ACK, and if B wants to send data at this time, send it (for example, do some pre-release processing)

(3) After B sends the data, it sends FIN to A. (At this time, B is in a semi-closed state)

(4) A sends ACK and enters TIME-WAIT state

(5) If the message from B is not received after 2MSL, it is determined that B has received the ACK. (At this time, A and B are completely closed)

Why wait for 2MSL?

The client sends the final ACK reply, but the ACK may be lost. If the server does not receive the ACK, it will continue to repeatedly send the FIN segment. Therefore, the Client cannot be closed immediately, it must confirm that the Server has received the ACK. The Client will enter the TIME_WAIT state after sending the ACK. Client will set a timer to wait for 2MSL time. If the FIN is received again within this time, the Client will resend the ACK and wait for 2MSL again. MSL refers to the maximum survival time of a segment in the network, and 2MSL is the maximum time required for a transmission and a reply. If the Client does not receive the FIN again until 2MSL, the Client concludes that the ACK has been successfully received and ends the TCP connection.

Understand the correct posture for four waves

  • Waved for the first time: A tells B that I have no data to send and I am about to close the connection. Do you want to send data?

  • Wave the second time: B sends a confirmation response, if there is data, then send the last data

  • Waves for the third time: B tells A, I also want to close the connection

  • Fourth wave: A tells B that you can close it, and I also closed it here

Guess you like

Origin blog.51cto.com/15082392/2656397