preface

Today we’re going to talk about the TCP three handshakes and four waves that interviewers often ask about during an interview. The five-tier architecture of the computer network was introduced briefly in the last article, so today we will talk about the transport layer TCP protocol three handshake and four wave.

Why three handshakes and four waves?

Some of you may be wondering, what is TCP’s three handshake and four wave? Why three handshakes and four waves?

When TCP transmits data, the Client and the Server establish a connection, and then segment the files to be transferred to provide reliable transmission and flow control. After the data transmission is complete, the current session must be disconnected to avoid resource waste. The TCP three-way handshake establishes the connection and the four-way wave disconnects the connection.

TCP Three-way handshake

How to better understand the TCP three-way handshake process is like I confess my love to my goddess.

Me: “Goddess, I like you for a long time, can you hear?” Goddess: “I can hear, I also like you, can you hear?” Me: “I can hear it too, so let’s be together.”

And then I was happy with my goddess. Let’s follow this diagram and talk about what happens with each handshake.

Byte oriented stream


Here are some fields: SYN: The synchronization sequence number, which is the handshake signal used to establish a connection. Ack: Confirmation sequence number. An ACK is valid if it is 1, and invalid if it is 0. Seq: Serial number. ACK: Confirm that the serial number is valid. FIN: Indicates the end flag, indicating that the connection is disconnected.

Initially, both the client and Server are in the Closed state, and the Server enters the LISTEN state before sending connection requests.

  • The first handshake: The Client sends a request segment to the Server, specifying the synchronization sequence number SYN = 1, ACK = 0, and the initial sequence number seq = x (seQ is the serial number of bytes). At the same time, the TCP Client process enters the SYN-sent state.
  • Second handshake: After receiving the SYN request packet from the client, the server sends a SYN packet to the client as a response, indicating that it agrees to set up a connection. In addition, the server sends its SYN = 1, ACK = 1, and SEq = y to the client to indicate its initial serial number. At the same time, it also tells the client where to start the next acknowledgement sequence. Since the initial serial number sent by the client is SEq = x, the acknowledgement sequence number ack = x + 1. At this time, the TCP server enters the SYN-RCVD state.
  • Third handshake: After receiving the acknowledgement packet from the server, the client sends another acknowledgement message to the server to indicate that the client has received the acknowledgement packet. So ACK = 1, seq = x + 1, ACK = y + 1. TCP establishes a connection, and the client and server enter the ESTab-Listend (established connection state) state.

Why do you shake hands three times, two times, four times?

Through the above three handshakes, we can clearly know the purpose of each step of the handshake.

The first handshake

The client sends the connection request segment, and the server receives it, so the server can tell that the client sent and received the request without any problems.

Second handshake

The server also sends a request segment, indicating that it has received the connection request segment from the client. At the same time, the client also receives the response segment from the server. In this way, the client can conclude that there is no problem between the server’s sending and its receiving. However, the server does not know whether the client received the message (the client only knows), so a third handshake is needed to notify the server.

The third handshake

The client then sends a request segment to indicate that it has received the acknowledgement from the server, and the server has received it. At this point, the handshake is over, and both sides can be known to send and receive normally.

If only shook hands before two times, will cause what kind of consequence? The client sends the connection request segment to the server for the first time, but it may be delayed due to network reasons. The client will consider the request invalid and send a connection request to the server again, and then the server will respond to connection normally. In a certain period of time, for the first time send a connection request arrives at the server, and if there is no third handshake confirmation, then the server will think he sent the client a new connection request, will be a response to the client, the client receives the response to a request, found that this request has been sent just now, but also received a response from the server, just ignore this request, However, the server is still waiting for the response from the client, which will waste resources.

What happens if you shake hands four times? After the third handshake, the server has received the last response request from the client. If the fourth handshake is made, the server should then respond to the request it just received. Whether the request failed or succeeded is meaningless. After the three-way handshake, both the client and the server know that the send and receive are normal and data can be transmitted, so there is no need to send the acknowledgement request again, and there is no completely reliable communication protocol.

TCP waves four times

Let me give you another example. A and B are A couple, one day, A said A to B: “let’s break up.” B(shocked) : “HMM.” . Silence ing… B:” Let’s break up.” A: “Yeah.” And then A and B are separated. Similarly, when a TCP connection is disconnected, four waves are required.

  • First wave: The client disconnects the connection, sends a FIN packet segment (FIN=1, seq= U) to the server, and the status changes to FIN-wait-1.
  • Second wave: The server responds to the request by sending an ACK = 1, SEQ = Y, ACK = U + 1(seQ and ACK have been explained in the three-way handshake phase) to indicate that it has received the disconnection request packet from the client. It also changes its state to close-wait.

After the client receives the second wave packet, will also own state changes to FIN – WAIT – 2 (closed WAIT), as the client disconnects the server connection, that is to say, the client can no longer send data to the server, but the server is not disconnected, it still wants to send the client did not send the data, That is, TCP is half closed, which is why four waves are needed.

  • Third wave: Wait for a period of time. When the server sends the rest of the data, it also sends a FIN = 1 segment to the client, with SEq = w, ACK = U + 1, ACK = 1. Meanwhile, it enters the last-ACK state to wait for the client to ack.
  • The fourth wave: The client responds to the server by sending ACK = 1, SEQ = U + 1, AND ACK = W + 1 packets to the server. The client enters the time-wait state. After a period of TIME, the server receives the response packet from the client. After the client waits for the TIME-WAIT period, the client also becomes Closed. After four waves, the client is disconnected from the server.

Why does the client go into a time-wait state after the last wave?

If the client for the last time to wave to send the confirmation request message, the server did not receive, will think of a service, because its own FIN message not sent, sent to the client not received, the client didn’t receive, will not send it to confirm the request message, then, the server will once again send a FIN message segment, So there is a waiting time of 2MSL.

conclusion

TCP intermediate state transition is a very complex process, I just I can understand the sorting out, so that you can refer to, if there is something wrong, welcome to leave a message.