The TCP three-handshake and four-wave question is one of the most common questions asked in an interview. Many readers know about triple and quadruple, but if you ask them a little deeper, they often can’t give an accurate answer.

This article attempts to illustrate this point with animation, in the hope that it will make it easier for readers to understand the nature of TCP interactions.

TCP three-way handshake

The TCP three-way handshake is like two people who see each other 50 meters apart on the street, but cannot be 100% sure because of smog and other reasons, so they have to wave to make sure that the other person knows them.

Jansan first waves to Jansan (SYN), and When Jansan waves to him, Jansan nods and smiles (ACK). Mr Sabic saw her smile and confirmed that she had identified herself.

But Li Si still a little suspicious, looked around, there is no possibility that Zhang SAN is looking at someone else, he also need to confirm. Therefore, Sam also waves to Sam (SYN). When Sam sees him waving to him, he knows that the other person is seeking his confirmation, so he also nods and smiles (ACK). After seeing the other person’s smile, Sam confirms that Sam is actually saying hello to him (enter the established state).

The two walked faster, came together, and hugged each other.

We see this process is a total of four actions, Three waved — Li Four nodded and smiled — Li Four waved — Zhang Three nodded and smiled. Li Si performed two movements in succession, first nodding and smiling (reply), and then waving again (seeking confirmation). In fact, the two movements can be combined, nodding and smiling at the same time (SYN + ACK). So the four movements simplified into three movements, Zhang SAN waved — Li Si nodded and smiled and waved — Zhang SAN nodded and smiled. This is the essence of the three-way handshake, with the middle movement being a combination of two movements.

We see that there are two intermediate states, SYN_SENT and SYN_RCVD, which are called “half on” and are waving at each other without seeing the other person nod and smile. Syn_sent is the “half open” state of the active open party, and SYN_RCVD is the “half open” state of the passive open party. The client is the active open side, the server is the passive open side.

  • syn_sent: syn package has been sent
  • syn_rcvd: syn package has been received

TCP data transmission

TCP data transmission is a conversation between two people at a distance, so you need to make sure that the other party heard you.

Zhang SAN shouted a word (data), Li Si heard after the reply to Zhang SAN heard (ACK).

If Zhang SAN shouted a long time did not hear Li Si reply, Zhang SAN will think his words were blown away by the wind, Li Si did not hear, so need to shout again, this is TCP retransmission.

It is also possible that Li Heard Zhang, but li’s reply to Zhang was blown away by the wind, so that Zhang didn’t hear Li’s reply. Zhang SAN can not judge whether his words were blown away by the wind or Li Si’s reply was blown away by the wind, Zhang SAN also need not care, retransmission is.

Since it will retransmit, It is possible that Li Si heard the same sentence twice, which is “to retransmit”. “Retransmission” and “retransmission” working operating system network kernel modules have been handled for us, the user layer is not concerned.

The TCP connection is “duplex”, so both parties can initiate data transmission. But no matter which party propaganda, need to receive the other party’s confirmation to consider the other party received their propaganda.

Zhang SAN may be an anti-aircraft gun, said even said eight sentences, at this time Li Si can not a sentence of a reply, but after listening to the eight sentences, together to the other side of the reply said the front you said eight sentences I heard, this is batch ACK. However, John cannot talk too much at one time, because John’s brain may not be able to digest too much in a short time. The two need to negotiate an appropriate sending and receiving rate, which is the “TCP window size”.

The data interaction in the network environment is more complicated than the dialogue between human beings, which has the phenomenon of packet out of order. Different packets from the same source may take different routes on the “Internet route” and end up in the same place in a different order. The network kernel module of the operating system is responsible for sorting the packets, and the order is exactly the same at the user level.

TCP waved four times

The process of breaking a TCP connection is similar to the process of establishing a connection, except that the middle two parts do not always move in one step, so it is divided into four actions, Wave (fin) – Smile (ACK) – wave (fin) – smile (ACK).

The two actions in the middle are not merged because TCP has a “half-closed” state, which is a one-way shutdown. Joe had waved his hand, but the man was still there. He just stopped talking, but his ears continued to listen. Waiting for Li Si tired, also no longer talk, waved toward Zhang SAN, Zhang SAN sad smile once, just completely over.

There is a very special state, time_wait, which is a long-term state that the closed party enters after replying to the other party’s wave. The standard duration of this state is 4 minutes. After 4 minutes, it will enter the closed state and release the socket resources. However, this time can be adjusted in the implementation.

It’s like the responsibility of the uninitiated breakup, you’re the one who asked for the breakup, and you have to pay the price. The consequence is a 4-minute time_wait in which socket resources (ports) cannot be released, similar to the widowhide period during which socket resources (ports) cannot be recycled.

It retransmits the last ACK packet to ensure that the peer party can receive it. If the peer does not receive an ACK, the FIN packet is retransmitted. The socket in time_wait state immediately resends an ACK packet to the peer.

At the same time, residual packets generated by the link on the Internet route during the conversation (the path is too rugged, the data packet takes too long, and the retransmitted packets are received, but the original packets are still on the road) are immediately discarded. Four minutes is enough time for the residual packets to disappear completely. Otherwise, residual packets may interfere with new links when new ports are reused.

4 minutes is 2 MSL, each MSL is 2 minutes. MSL is maximium segment lifetime. This time is defined by the official RFC protocol. I haven’t seen a very satisfactory explanation as to why 2 MSL instead of 1 MSL.

The four waves are not always four waves. Sometimes the two movements in the middle can be combined to create a three wave, and the active closing party will go directly from fin_WAIT_1 to time_wait, skipping fin_WAIT_2.

conclusion

TCP state transition is a very complex process. This article only explains some simple basic knowledge points by analogy. More on TCP requires readers to search technical articles for further study. If the reader has a solid grasp of THE basics of TCP, the more advanced knowledge will not be too difficult to understand.

For more articles on TCP, please follow the wechat official account “Code Hole” to subscribe, and I will update more details in the future.

If you think the quality of this article is ok, please give me some props for my nuggets booklet

In-depth understanding of RPC In-depth understanding of RPC

In-depth understanding of RPC In-depth understanding of RPC