当前位置:网站首页>TCP shakes hands three times and waves four times. Do you really understand?

TCP shakes hands three times and waves four times. Do you really understand?

2022-07-04 21:34:00 SS, shuaihai

I believe you are in the process of learning , We all know TCP Three handshakes of , Four waves , But I can't remember it very clearly , Then this article will take you to review these two processes , Okay , Text begins


TCP Three handshakes

Love simulation

Take falling in love as an example , The most important thing for two people to be together is to first confirm their ability to love and be loved . Next, let's simulate the process of shaking hands three times .

for the first time :

male : I love you! .

Woman received .
This proves that the man has Love The ability of .
 

The second time :
Woman : I received your love , I love you too .

The man received .
OK, The current situation shows that , The woman has Love and To be loved The ability of .

third time :
male : I received your love .

Woman received .
Now it's guaranteed that the man has To be loved The ability of .

Thus, the two sides are completely confirmed Love and To be loved The ability of , They started a sweet love .

Real handshake
 

Of course, that was bullshit , It doesn't represent my values , The purpose is to make people understand the meaning of the whole handshake process , Because the two processes are very similar . Corresponding to TCP Three handshakes of , We also need to confirm the two abilities of both sides : The ability to send and The ability to receive . So there will be the following three handshakes :
 

  From the beginning, both sides were in CLOSED state . Then the server starts listening to a port , Into the LISTEN state . The client then initiates the connection , send out SYN, I became SYN-SENT state .
The server receives , return SYN and ACK( Corresponding to the SYN), I became SYN-REVD.
Then the client sends ACK To the server , I became ESTABLISHED state ; Server received ACK after , Also became ESTABLISHED state .
Another thing to remind you of is , As you can see from the diagram ,SYN It needs to consume a serial number , Next time send the corresponding ACK The serial number needs to be added 1, Why? ? Just remember one rule :

All that need to be confirmed by the end , Must consume TCP The sequence number of the message .

SYN Need end-to-end validation , and ACK It doesn't need to , therefore SYN Consume a serial number and ACK Unwanted .



Then why three times instead of two , The four time ?

(1) Why not twice

The root cause : Unable to confirm the receiving capability of the client .

The analysis is as follows :
If it's twice , You're sending SYN I want to shake hands , But this bag retention In the current network has not arrived ,TCP I thought it was a lost bag , So it was repeated , Two handshakes make the connection .
There seems to be no problem , But when the connection is closed , If this retention The packets in the network arrive at the server ? At this time, because it's two handshakes , The server only needs to receive and send the corresponding packet , By default Establishing a connection , But now the client is disconnected . See the problem , This leads to a waste of connection resources .

(2) Why not four times

The purpose of three handshakes is to confirm that both sides Send and receive The ability of , Four handshakes are OK ?
Certainly. ,100 Every time . But to solve the problem , Three times is enough , No matter how much, it won't be of much use .
 

           

Can I carry data during the three handshakes

The third handshake , Can carry . The first two handshakes don't carry data .
If the first two handshakes can carry data , So once someone wants to attack the server , So he just needs to shake hands for the first time SYN There's a lot of data in the message , So the server is bound to consume more Time and memory space To process the data , Increased the risk of server being attacked .
The third handshake , The client is already in ESTABLISHED state , And has been able to confirm the server's reception 、 Normal sending capacity , It's relatively safe at this time , Can carry data .
 

What if I open it at the same time

If both sides send at the same time SYN message , What will the state change like ?

This is a possible situation .
The state changes as follows :

 

Send... From the sender to the receiver SYN At the same time , The receiver also sends SYN message , Two people just got on !

End of hair SYN, The state of both becomes SYN-SENT.
After receiving each other's SYN after , Both states become SYN-REVD.
Then it will reply to the corresponding ACK + SYN, This message is received by the other party , The two states together become ESTABLISHED. This is the state transition in the case of simultaneous opening .
 


TCP The process of waving four times

Process disassembly

  At first, the two sides were in ESTABLISHED state .
The client is about to disconnect , Send to server FIN message , stay TCP The position in the message is shown in the figure below :

  After sending, the client becomes FIN-WAIT-1 state . Be careful , At this time, the client also becomes half-close( Half closed ) state , That is, it is unable to send messages to the server , receive calls only .
After receiving, the server confirms to the client , Turned into CLOSED-WAIT state .

The client receives the confirmation from the server , Turned into FIN-WAIT2 state .

And then , The server sends... To the client FIN, Enter on your own LAST-ACK state ,
The client received a message from the server FIN after , I became TIME-WAIT state , And then send ACK To the server .
Pay attention , This is the time , The client needs to wait long enough , say concretely , yes 2 individual MSL (Maximum segment
Lifetime, Maximum message lifetime )
, During this period, if the client does not receive the resend request from the server , So expressed ACK Successfully arrived at , Wave over , Otherwise, the client will resend ACK.

wait for 2MSL The meaning of

What happens if you don't wait ?
If you don't wait , The client runs directly , When the server still has many packets to send to the client , And on the road , If the client's port is occupied by a new application at this time , Then you receive useless packets , Cause packet confusion . therefore , The safest way is to wait for the packets from the server to die before starting a new application .
that , In this way, a MSL It's not enough , Why wait 2 MSL?

  • 1 individual MSL Make sure the last of the four waves is closed ACK Finally, the message can reach the opposite end
  • 1 individual MSL Make sure the opposite end doesn't receive ACK Retransmission FIN Messages can reach
     

This is waiting 2MSL The meaning of

Why four waves instead of three

Because the server receives FIN, Often they don't return immediately FIN , You have to wait until all the messages on the server are sent , To send FIN. So start with a ACK Indicates that the client has received FIN, Delayed for a while FTN. That's four waves . If it's three waves, what's the problem ?
It means that the server will ACK and FIN The send is combined into a wave , This long delay may lead to the client mistaking FIN Didn't reach the client , So that the client can continue to resend FIN.
 

What will happen if it is closed at the same time

If the client and server send at the same time FIN, How the state will change ? As shown in the figure :

 


Okay , That's the end of this article , If you think it's useful, give it a compliment

原网站

版权声明
本文为[SS, shuaihai]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042034010593.html