A short story to tell clearly TCP three-way handshake and four-way handshake

There are many articles about the TCP three-way handshake, but I always feel a bit abstract. This time, I plan to explain the three-way handshake and four-way handshake through a short story of "printer".

1. Three-way handshake

TCP provides connection-oriented services, and a connection must be established before transmitting data. A TCP connection is established through a three-way handshake.

1.1 Standard Description

Three-way handshake process
Three-way handshake process:

  • At the beginning, both parties are in the closed state. When the server listens to the client's request, the state of the server will enter the listen state.
  • The first handshake: the client sends a SYN message to the server, and indicates the client's initialization serial number ISN (seq=x), at this time the client is in the SYN_SENT state
  • The second handshake: After receiving the SYN message from the client, the server responds with its own SYN message and specifies the initialization sequence number (seq=y) of its own server. In addition, in order to confirm the SYN message of the client, the ISN (that is, x)+1 is used as the value of ack (ack=x+1). At this time, the server is in the SYN_RCVD state
  • The third handshake: After the client receives the SYN message, it will send an ACK message, which will use the ISN of the server (that is, y)+1 as the ack value (ack=y+1). At this time, the client In the ESTABLISHED state. After the server receives the ACK message, the server also enters the ESTABLISHED state. At this time, the connection is established and communication can start

1.2 "Printer Story"

TCP three-way handshake popular process
Once at work, Xiao Zhao wanted to use a printer to print some materials, but his computer did not have a driver, so he wanted to ask Xiao Wang to help print it.

  • The first handshake: Xiao Zhao called first: "I am Xiao Zhao, Xiao Wang, are you there?"
  • The second handshake: Xiao Wang heard that it was Xiao Zhao, and then replied: "Xiao Zhao, I am Xiao Wang, I am here, what's the matter?"
  • The third handshake: Xiao Zhao heard that Xiao Wang replied, proving that he can ask for help, and then hurriedly said: "Xiao Wang, I need to print materials, and I will send you materials later, just wait for me for a while"
  • Then Xiao Wang knew that Xiao Zhao was going to print something, so he started to prepare the printer, and waited for Xiao Zhao to send out the materials

1.3 Why is the TCP handshake three times, why not two?

1. It is to prevent the server from opening useless connections to increase the overhead of the server.
2. To prevent the invalid connection request segment from suddenly reaching the server and causing an error.

1.3.1 Prevent the server from establishing useless connections and increase server overhead

Due to the possibility of network congestion in network transmission , if there is no third handshake after the server sends the SYN message, the server does not know whether the message can be received by the client. In this case, the server will It will think that the current port is available, and the port will always be open. When the client re-sends a request due to timeout, the server will reopen a port connection. As a result, many invalid connection ports will be opened in vain, resulting in a waste of resources .
insert image description here

To put it simply, Xiao Wang replied "Xiao Zhao, I'm Xiao Wang, I'm here" after hearing Xiao Zhao's call. Then Xiao Zhao didn't reply any messages. Xiao Wang didn't know if Xiao Zhao heard the reply I don't know what Xiao Zhao will do in the future, so I can only wait, which makes Xiao Wang unable to do other things.

1.3.2 Prevent the invalid connection request segment from suddenly reaching the server again, causing an error

Another situation is that the request information sent by the invalid client is transmitted to the server for some reason, and the server thinks it is a valid request sent by the client, and an error occurs after receiving it.
insert image description here
So we need a "third handshake" to confirm the process:

The data of the third handshake tells the server whether the client has received the data sent by the server during the "second handshake" and whether the serial number of this connection is valid. If the data sent is "received and there is no problem", the server will normally establish a TCP connection after receiving it, otherwise the establishment of a TCP connection fails, and the server closes the connection port. This reduces server overhead and errors from receiving invalid requests.

1.4 Why can't the TCP handshake be four times?

Simply put, three handshakes are enough to create a reliable connection, and there is no need for one more handshake to take more time to establish a connection

1.5 What will happen if no message is received in the three-way handshake

  • The first handshake server did not receive the SYN message

The server will not take any action , and the client will resend the SYN message after waiting for a period of time because it has not received the confirmation message from the server for a period of time. If there is still no response, it will repeat this process until If the number of sending times exceeds the maximum number of retransmissions, a connection establishment failure will be returned.

  • The second handshake client did not receive the ACK message responded by the server

The client will continue to retransmit until the number of times is limited; at this time, the server will be blocked at accept() , waiting for the client to send an ACK message

  • The third handshake server did not receive the ACK message sent by the client

The server will also adopt a timeout retransmission mechanism similar to that of the client . If the number of retries exceeds the limit, the accept() call will return -1, and the server will fail to establish a connection;

At this time, the client thinks that the connection has been successfully established, so it starts to send data to the server, but the accept() system call of the server has returned, and it is not in the listening state at this time, so when the server receives the data sent by the client, it will Send an RST message to the client to eliminate the state of the client unilaterally establishing a connection.

2. Wave four times

2.1 Standard Description

Four wave process:

insert image description here

Four wave process:

  • After the data transmission is over, both communication parties can actively initiate a disconnection request. Here, it is assumed that the client initiates a disconnection request.
  • The first wave: the client sends a disconnected FIN message, specifying a serial number (seq=u) in the message, and the client is in the state of FIN_WAIT_1
  • The second wave: After the server receives the FIN message, it sends an ACK message, specifying a sequence number of its own server (seq=v) and the value of the received client sequence number + 1 as ack Value (ack=u+1), indicating that the message from the client has been received, and the server is in the CLOSED_WAIT state at this time
  • The third wave: the server sends a FIN message to release the connection. Like the first wave, the client specifies its own sequence number (seq=w), and the server is in the LAST_ACK state .
  • The fourth wave: After receiving the FIN message from the server, the client sends an ACK message (ACK=1 seq=u+1 ack=w+1) as in the second wave, and the client is in the TIME_WAIT state .After waiting for a certain fixed time (two maximum segment life cycles, 2MSL, 2 Maximum Segment Lifetime) , the server does not receive the ACK, and thinks that the server has closed the connection normally, so it also closes the connection and enters the CLOSED state. After receiving the confirmation packet, the server closes the connection and enters the CLOSED state.

2.2 Follow-up of "Printer Story"

Last time I mentioned that Xiao Zhao asked Xiao Wang to help print the materials, but the materials are always finished. At this time, Xiao Zhao found that all the materials that need to be printed have been sent to Xiao Wang, so he was going to tell Xiao Wang that it was almost time to finish

  • First of all, Xiao Zhao told Xiao Wang that all the materials have been sent to you, that’s all, there is no need to type anymore, and the skills are almost ready
  • After Xiao Wang received the message, he replied that I know, there are still a few printers in the printer, and they will call it a day.
  • After all the materials are printed, Xiao Wang tells Xiao Zhao that they are all printed, and you can see if there are any missing, do you need to reprint?
  • Xiao Zhao checked and found that there was no shortage of things. He told Xiao Wang that everything was complete and there was a lot, so he could pack it up and go back. Then Xiao Zhao waited for a while and saw that there was no news from Xiao Wang, so he packed up his things and left. As for Xiao Wang, after receiving Xiao Zhao's notification that it could end, he packed up and left.

insert image description here

2.3 Why four waves are needed

It is obvious from the "Printer Story" that the server will not close the connection immediately after receiving the client disconnection FIN message, but first sends an ACK packet to tell the client to receive the request to close the connection. Only after all the messages of the server are sent , the FIN message is sent to disconnect, so four waves are required.

2.4 Why do we need to wait for 2MSL

  1. In order to ensure that the last ACK segment sent by the client can reach the server . There is a problem of packet loss in the network. I am afraid that the last ACK message sent by the client will be lost, which will cause the server to stay in the LAST_ACK state because it has not received the message and cannot be closed. If the server does not receive the message for a period of time, it will Resend the FIN message. At this time, after the client receives the message, it will resend the ACK message and reset the waiting time to 2MSL. Until the end, both the client and the server enter the CLOSED state

  2. Prevent invalid connection request segments from appearing in this connection . After 2MSL time, all segments generated during the duration of this connection can disappear from the network. In this way, no This old connection request segment will appear.

2.5 Why is the waiting time 2MSL?

MSL (Maximum Segment Lifetime), the maximum message lifetime. It is the longest time for any message to exist on the network, and the message will be discarded after this time.

As for why it is 2 times, it is considered that there is a situation where both parties send. The client sends a message first, reaches the server after an MSL, and then the server responds, and reaches the client through an MLS. It is exactly 2MSL for one to one return
insert image description here

3. Overall process

A complete three-way handshake and four waved hands are shown in the following figure:
insert image description here

references

  1. https://mp.weixin.qq.com/s/yAlErlC09GnjaVvwUo3Acg
  2. https://vue3js.cn/interview/http/handshakes_waves.html#%E4%B8%80%E3%80%81%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B
  3. https://juejin.cn/post/6908327746473033741#heading-82

Guess you like

Origin blog.csdn.net/baidu_33438652/article/details/125458696