PS: The more mundane explanation will be in the reference block

Nothing is true, Everything is permitted.

0. What is TCP

Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte stream based unicast Protocol. Different from TCP/IP, TCP/IP is a protocol family that involves the network layer, application layer and application layer in the OSI model. And THE TCP that we’re talking about is the protocol at the transport layer, and HTTP requests, which are very popular today, are based on TCP.

1. Three handshakes

Connection-oriented is easy to understand, as if we want to issue a command to a remote server, and first we need to log in. This login is the process of connecting.

Before data can be exchanged, communication parties must establish a connection with each other. Both parties maintain a copy of each other’s information, such as IP addresses and port numbers. When it comes to connecting, there’s the classic three-way handshake and four-way wave.

1.1 Why not shake hands twice

The three-way handshake makes it clear to both parties that a connection is being established and also ensures that both the client and server have the ability to send and receive. Two handshakes don’t do that. Now let’s look at the three handshakes from another point of view, which is why three handshakes? Doesn’t it smell good when I shake hands twice? Let’s use a dialogue to simulate the problems that would arise if we did adopt a two-handshake.

Friend: Hello, hello? Listen to get?

You: Yes… Could you please keep your voice down

That’s two handshakes.

In human logic, this is already a normal conversation, right? Isn’t the next step to establish a connection? Before you go to the next step, you need to understand the purpose of the three-way handshake. The three-way handshake makes it clear to both parties that a connection is being established and also ensures that both the client and server have the ability to send and receive.

Let’s analyze the conversation above.

If a friend asks you if you can hear him, he has the ability to send. If you hear a friend’s question, you’re receptive

If there are only two handshakes, what’s the problem?

In your friend’s shoes, he knows that you can send and receive at the same time

But from your point of view, you only know that your friend has the ability to send, because you don’t know if your voice can be turned down or if he can receive it

If the server does not know whether the client is capable of receiving the data, it cannot know whether the client has received the data even if the packet is actually sent. This is an unreliable connection.

Moreover, in real network transmission, network delay is common. If the client sends a packet requesting to establish a connection, the packet does not arrive due to the network delay, the client sends another packet, and the server receives the packet and establishes the connection.

However, after the current connection is closed, the server establishes the connection again due to the network delay, but the client has been disconnected, thus wasting the resources of the server.

If the above example still doesn’t make sense to you, why not shake hands twice? Here is the ultimate example.

Friend: lend me some money, XX treasure account 123XXXXXXXX

You: Ok, is your account number 123XXXXXXXX

. (No answer)

Inside you: ??????

If you were the one being lent the money, would you dare to transfer it?

0341fda0555dc92b70a2ea4874115d5b

Here’s a quick summary of the problems with two handshakes: unreliable and a waste of network resources.

1.2 The process of three handshakes

Above we discussed why a triple handshake is necessary, but let’s use a few technical terms to explain the process.

  • The server listens for a port and enters the LISTEN state

  • The client is initially CLOSED and then sends a packet with the SYN flag bit to the server to initiate a connection. The client became syn-sent. Procedure

  • The server receives the SYN packet from the client and knows that the client wants to establish a connection through the flag bit. A SYN and an ACK are sent back to indicate that the request was received. The status of the server changes to SYN-RCVD

  • The client receives an ACK from the server indicating that the server knows that the client wants to establish a connection. The client then sends an ACK back to the server indicating that it received (or was able to receive) the server message. After sending the ACK, the client state changes to “ESTABLISH”

  • The server receives an ACK from the client and changes its state to ESTABLISH

2. Wave four times

2.1 Simulate four wave hands

As usual, let’s simulate TCP’s four waves with a conversation.

Scene, you’re out partying with your friends

You: you continue to play, I walk first, tomorrow also go to work (the first time)

Old iron :(old iron saw you talking to him and knew you were leaving from your words, old iron also used body language to tell you that he knew you were leaving) (second time)

Old tie: All right, be safe on the road. (for the third time)

You: Ok, next time (fourth time)

This is the popular version of the four waves explained, from a professional point of view.

2.2 The process of four waves

Let’s take a look at the complete process.

  • Initially, both the client and server are in the ESTABLISH state

  • If the client wants to disconnect the connection, it proactively sends the packet with flag bit FIN to the server. After the packet is sent, the client state changes to Fin-WaIT-1 and the client state changes to half-closed. That is, the client cannot send data packets to the server and can only receive data from the server

  • The server receives the FIN packet from the client, changes its status to close-wait, and sends an ACK packet to the client

  • After the client receives an ACK, the state changes to FIN-WaIT-2

  • Then, the server sends a FIN packet to the client, and the server status changes to last-ACK

  • Description The client receives the FIN packet, and the client state changes to time-wait. An acknowledgement ACK is then sent back to the server

  • The client then waits for 2MSL. If it does not receive a resend message from the server within this time, the server has received an ACK

  • Four waves end here and the connection is disconnected

Let’s do that again.

Scene, you’re out partying with your friends

You: you continue to play, I walk first, tomorrow also go to work (the first time)

Old iron :(old iron drunk high, reflex arc extended indefinitely)

You had to say it again. You told your friend you were leaving, so you said it again.

You: you continue to play, I walk first, tomorrow also go to work (the first time)

Old iron :(old iron drunk high, reflex arc extended indefinitely)

.

So again and again

In fact, the ACK and FIN packets sent by the server to the client are merged into the same wave twice. If the network delay occurs, the client thinks that the FIN packet did not reach the server and resends the packet. If the delay is long, the client resends TCP packets sent by the FIN.

2.3 Comparative Analysis

The combination of the abstract and the concrete four waves makes a lot of sense, but let’s summarize it in a table.

Describe the state The actual situation
You’re out partying with your friends The client establishes a connection with the server
You told your friends you were leaving The client proactively sends a FIN message to the server, and the client status changes to FIN-WaIT-1
Your friend hears and understands what you have to say, and he gets it back to you through body language The server receives the FIN packet and replies with an ACK. The server status changes to close-wait. The client changes to FIN-WaIT-2 after receiving the ACK
Your friend says, “Well, be safe on the road.” When the server sends a FIN packet to the client, the server changes to last-check.
You say, “Okay, I’ll see you next time.” The client status changes to time-wait after receiving the FIN packet. And send an ACK back to the server.
You hesitated, but your friend did not detain you The client waits for 2MSL, and if it does not receive a resend message from the server, the server has received an ACK.
You left a party with friends Four waves end the connection

2.4 Why wait

MSL: Maximun Segment LifeTime: indicates the maximum LifeTime of a packet. Why WAIT 2MSL after time-wait? There are two main reasons. Let’s use examples to understand.

Ensure that the server receives an ACK

Let’s say you say “Yes, I’ll see you later.” It was too loud because everyone was high. So your friend doesn’t hear you say “ok, we’ll see you later” and you walk away.

You know, if you were in your friend’s shoes, you’d feel terrible, reminding you, and not even saying good-bye?

In this case, the server does not receive an ACK packet from the client and does not know that the client has received the FIN packet. It will not be disconnected, but the client has been unilaterally disconnected. The server wasted resources, and the server can not enter the normal closed connection state.

Preventing invalid packets

Similarly, after you say “ok, I’ll see you later,” you walk away without checking to see if your friend heard you. Your friend also drank too much, and now he is very upset and says something silly.

This sentence happened to be passing by, standing in the same position you just stood on the buddy caught, thought that he was talking about him, the in the mind is very uncomfortable, fists to beat your friend.

In this case, the client is disconnected without waiting for 2MSL, but the server still has some packets to send or has already sent. However, after the packet arrives, the port is occupied by the new connection, and the old TCP packet conflicts with the TCP packet of the new connection.

3. The end

If I have time later, I will continue to try to abstract boring theories into some simple phenomena in life and combine them with professional knowledge in an essay style to help those who find it difficult to read theoretical knowledge. In fact, as long as you understand the whole idea, there is no need to memorize.

If there is wrong place in the article, but also hope you big guy not stingy give advice.

If you find this article helpful, please give it a thumbs up, a comment, a share and a comment

You can also search the official account [SH full stack Notes] on wechat, and of course you can also scan the TWO-DIMENSIONAL code to follow it

Thanks to a worship