TCP shakes hands three times and waves four times. In the boiling oil of the interview, it can be said that it is an expert who has been fried hundreds of times.

We all know that TCP is connection-oriented, that a three-way handshake is used to establish a connection, and a four-way handshake is used to disconnect a connection.

Three-way handshake

First above:

Let’s take a look at the three-way handshake:

  • In the beginning, both the client and the server are inCLOSEDState. The client opens the connection actively, the server cards the connection passively, and the connection endsCLOSEDState Z, start listening, enterLISTEN State.

A handshake

  • The client will randomly initialize the sequence number (client_isn), put the sequence number in the sequence number field of the TCP header, and put theSYNThe mark position is1Said,SYNMessage. Then the first SYN packet is sent to the server to initiate a connection to the server. The packet does not contain application-layer dataSYN-SENTState.

Secondary to shake hands

  • The server received the information from the client. ProcedureSYNAfter the packet is sent, the server initializes its serial number randomly (server_isn), enter the serial number in the SERIAL number field of the TCP header, and then enter the Acknowledgement reply number field of the TCP headerclient_isn + 1And then theSYNACKThe mark position is1. Finally, the packet is sent to the client, which does not contain application layer data, and then the server is inSYN-RCVDState.

Three-way handshake

  • After receiving the packet from the server, the client sends the last reply packet to the server. The first response packet is the TCP headerACKThe mark position is1Next fill in the “Confirm response number” fieldserver_isn + 1This time, the packet can carry the data of the client to the server. After that, the client is in theESTABLISHEDState.

Ok, after the three-way handshake, the connection between the client and the server is ESTABLISHED, and the server and client are happily communicating.

Here’s a diagram of the dynamic process:

Here’s a little detail. The third handshake is data-free. This is a common interview point.

So why the three handshakes? Not twice?

  • In order to prevent the server to open some unnecessary connections to increase the cost of the server
  • Prevent an invalid connection request segment from being suddenly transmitted to the server, resulting in an error.

Because network traffic is delayed (through network fibers and various intermediate proxy servers), during transmission, for example, a client initiates a SYN=1 first handshake.

If the server creates the connection directly and returns a packet containing the SYN, ACK, and Seq to the client, the packet is lost due to network transmission, and the client never receives the packet from the server.

The server does not know if the client received the message if there is no third handshake to tell the server that the client received the message. The server assumes that the connection is available and the port is left open until the client resends the request due to a timeout and the server reopens a port connection.

As a result, there are many invalid connection ports left open, resulting in a waste of resources.

This process can be understood as:

Another case 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 generates an error after receiving it.

So we need a “third handshake” to confirm the process:

The data from the third handshake tells the server whether the client received the data from the server’s “second handshake” and whether the connection number is valid. If the data sent is a “received and no problem” message, the server normally establishes the TCP connection after receiving the message. Otherwise, the TCP connection fails to be established and the server closes the connection port. This reduces server overhead and errors that occur when invalid requests are received.

Four times to wave

Let’s start with the picture above:

Sometimes a TCP connection is disconnected by four waves of the hand.

Both hosts can disconnect the connection. After the connection is disconnected, the resources on the host are released.

Above is the client actively closing the connection:

A wave

  • When the client intends to close the connection, it sends a TCP headerFINThe flag bit is set to1, that isFINMessage, after which the client entersFIN_WAIT_1State.

Second wave

  • After receiving the packet, the server sends it to the clientACKResponds to the packet, and then the server entersCLOSED_WAITState.

Three wave

  • The client received the information from the server. ProcedureACKAfter the packet is answered, it entersFIN_WAIT_2State. After the server finishes processing the data, it also sends the data to the clientFINPacket, after which the server entersLAST_ACKState.

Four times to wave

  • The client received the information from the server. ProcedureFINAfter the packet is sent, a reply is receivedACKResponds to the packet and then entersTIME_WAITstate
  • The server received it.ACKAfter the packet is answered, it entersCLOSEDThe server has completed closing the connection.
  • The client is passing2MSLAfter a period of time, automatic entryCLOSEDState, at which point the client has completed closing the connection.

As you can see, each direction requires a FIN and an ACK, so it is often referred to as a four wave.

Why four waves?

Let’s review the process of sending the FIN bag after four waves, and we can understand why we need four waves.

  • When the connection is closed, the client sends a message to the serverFIN“Simply means that the client is no longer sending data but can still receive data.
  • The server received the client. ProcedureFINBefore sending a packet, reply one packet firstACKThe server sends the response packet only when the server does not send any more dataFINThe message is sent to the client to agree to now close the connection.

As can be seen from the above process, the server usually needs to wait for the data to be sent and processed. Therefore, the ACK and FIN of the server are usually sent separately, resulting in one more time than the three-way handshake.

Why does the client WAIT 2MSL in the time-wait phase?

This is to check whether the server receives the ACK acknowledgement packet sent by the client. When the client sends the final ACK acknowledgement packet, it is not sure whether the server can receive the packet.

Therefore, the client will set a timer with a duration of 2MSL after sending an ACK acknowledgement packet.

MSL Indicates the Maximum Segment Lifetime of a TCP packet during transmission.

2MSL indicates the maximum validity period for FIN packets sent by the server and ACK confirmation packets sent by the client.

If the server does not receive an ACK packet from the client within 1MSL, it sends a FIN packet to the client again:

  • If the client in 2MSL receives a FIN packet from the server again, the server does not receive the ACK packet from the client due to various reasons.

The client sends an ACK packet to the server again, and the timer is reset to restart the 2MSL timer.

  • Otherwise, the client does not receive the FIN packet from the server again within 2MSL, indicating that the server has received the ACK packet normally. The client can enter the CLOSED phase and complete the four-wave motion.

Therefore, the client must undergo a time-wait phase of 2SML. This is why the client side enters the CLOSED phase later than the server side.

Here is also a diagram of the dynamic process:

Well, that’s where our article ends…

Oh, no, that’s it. Right now I think I know, but maybe later.

It doesn’t matter. I thought hard and found two examples in plain English, so you can’t forget them.

Three handshakes in plain English

In the countryside twenty years ago, the telephone was not popular, not to mention the mobile phone, so communication basically relies on Shouting.

Lao Zhang and Lao Wang were neighbors. This day, Lao Zhang was on the ground. As a result, there was something wrong with his family.

  • Lao Wang: Lao Zhang alas! This is Wang, can you hear me?
  • “Lao Wang Lao Wang, this is Lao Zhang, I can hear, can you hear?
  • Lao Wang: Lao Zhang, I heard you. I have something to tell you.

“Your wife is having a baby, so hurry home!”

Lao Zhang rushed home, his wife successfully gave birth to a big boy with a handle.

The story of the handshake is full of happiness and happiness.

Four waves

If the blogger has a girlfriend-just “if”, damnit, the tears can’t stop dripping on the keyboard.

As a result of the blogger to go to work, work liver blog, lead to no time to accompany his girlfriend, girlfriend unbearable.

  • Girlfriend: smelly man, recently you all ignore me, do you not love me? Do you have another dog out there? I’m breaking up with you?
  • Sand sculpture blogger one leng, anger attack heart: break up and break up, do not accompany you to make, wait for me to pack up things.

The sand sculpture blogger carefully installed his green axis mechanical keyboard.

  • Well, silly woman, I’ve finished. Here’s to my ass. Bye!
  • Girlfriend: Roll, roll far away, the farther away the better, I never want to see you again all my life.

Alas, the story of waving is always full of sorrow and regret!


All right, it’s fun! For the blogger’s brainpower, just click “like” before you go!!




Reference:

[1] : Two GIFs – fully understand TCP’s three handshake and four wave

[2] : 35 diagrams: asked hundreds of times TCP three handshakes and four waves interview questions

[3] : Three handshakes + four waves, a piece to do all!