current position:Home>Explain the "three handshakes" and "four waves" of TCP connection in detail

Explain the "three handshakes" and "four waves" of TCP connection in detail

2022-04-29 13:03:29conner

TCP connection

image.png

image.png

In the process of sending and returning data between client and server, you need to create a call TCP connection Things that are ;

because TCP There is no concept of connection , There are only requests and responses , Requests and responses are packets , They all pass by TCP Create one from the client , Channel similar to the connection received by the server , This connection can be maintained all the time ,http The request is sent based on this connection ;

In a TCP You can send more than one on the connection http Requested , Different versions of this model are different .

stay HTTP/1.0 In this TCP The connection is in http It is created synchronously when the request is created ,http The request is sent to the server , After the server responds , This TCP The connection is closed ;

HTTP/1.1 In some way, you can declare that the connection has been kept , After a request is transmitted , Another request can then be transmitted . The advantage of this is : Creating a TCP The process of connecting requires “ Three handshakes ” Consumption of ,“ Three handshakes ” There are three times of network transmission .

If TCP The connection , The second request is sent without this “ Three handshakes ” Consumption of .HTTP/2 The same one TCP The connection can also be transmitted concurrently http request .

1. TCP Message format Introduction

image.png The more important fields are :

(1) Serial number (sequence number):Seq Serial number , Occupy 32 position , Used to identify from TCP Byte stream sent from source to destination , Flag this when the initiator sends data .

(2) Confirmation no. (acknowledgement number):Ack Serial number , Occupy 32 position , Only ACK Sign bit is 1 when , Verify that the ordinal field is valid ,Ack=Seq+1.

(3) Sign a (Flags): common 6 individual , namely URG、ACK、PSH、RST、SYN、FIN etc. . The specific meaning is as follows :

URG: Pointer to an emergency (urgent pointer) It works .ACK: Confirm that the serial number is valid .PSH: The receiver should send this message to the application layer as soon as possible .RST: Reset connection .SYN: Initiate a new connection .FIN: Release a connection .

It should be noted that :

Do not confirm the serial number Ack And in flags ACK Confused. . Confirmation party Ack= Initiator Seq+1, Pairing at ends .

2. TCP Three handshakes of (Three-Way Handshake)

2.1 ” Three handshakes ” Detailed explanation of

The so-called three handshakes are TCP Connection establishment . This connection must be opened by one party , The other side opens passively . The following is an illustration of the active connection initiated by the client :

image.png

Before handshaking, the client who actively opens the connection ends CLOSED Stage , Passively open the server end CLOSED Stage , And enter LISTEN Stage . Later on “ Three handshakes ”:

(1) First, the client sends a segment to the server TCP message , among :

  • The tag bit is SYN, Express “ Request a new connection ”;
  • Serial number is Seq=X(X It's usually 1);
  • Then the client enters SYN-SENT Stage .

(2) The server receives... From the client TCP After the message , end LISTEN Stage . And return to a paragraph TCP message , among :

  • Sign bit is SYN and ACK, Express “ Confirm the message of the client Seq The serial number is valid , The server can normally receive the data sent by the client , And agreed to create a new connection ”( Tell the client , The server received your data );
  • Serial number is Seq=y; The confirmation number is Ack=x+1, Indicates the serial number of the received client Seq And add the value to 1 As your own confirmation number Ack Value ;
  • Then the server enters SYN-RCVD Stage .

(3) The client receives the confirmation of receiving data from the server TCP After the message , It is clear that the data transmission from the client to the server is normal , end SYN-SENT Stage . And back to the last paragraph TCP message . among :

  • Sign bit is ACK, Express “ Confirm receiving the signal that the server agrees to connect ”( Tell the server , I know you received the data I sent );
  • Serial number is Seq=x+1, Indicates receiving the confirmation number from the server Ack, And take its value as its ordinal value ;
  • The confirmation number is Ack=y+1, Indicates receiving server-side serial number Seq, And add the value to 1 As your own confirmation number Ack Value ;
  • Then the client enters ESTABLISHED Stage .
  • The server received... From the client “ Confirm receipt of server data ” Of TCP After the message , It is clear that the data transmission from the server to the client is normal .
  • end SYN-SENT Stage , Get into ESTABLISHED Stage .

Transmitted between client and server TCP In the message , Confirmation number of both parties Ack And serial number Seq Value , It's all about each other Ack and Seq Calculated on the basis of values , This guarantees TCP Continuity of message transmission . Once there is a party sending out TCP Message lost , Can't go on " handshake ", This ensures that " Three handshakes " The successful completion of .

After that, the client and server carry out normal data transmission . This is it. “ Three handshakes ” The process of .

2.2 “ Three handshakes ” The popular understanding of

image.png

for instance : Compare clients to boys , Servers are like girls . Use their contacts to illustrate “ Three handshakes ” The process :

(1) Boys like girls , So I wrote a letter to tell the girl : I love you! , Please associate with me !; After writing the letter , The boy waited anxiously , Because I don't know if the letter can be delivered to the girl .

(2) When the girl received the boy's love letter , be wild with joy , It turns out that we are in love ! So I wrote a reply to the boy : I got your love letter , I know what you mean , Actually , I like you, too ! I would like to associate with you !;

After writing the letter , The girl is also anxiously waiting for , Because I don't know if the reply can be delivered to the boy smoothly .

(3) The boy was very happy after receiving the reply , The girl received the love letter , And I know from the reply that girls like themselves , And I'm willing to associate with myself . Then the boy wrote another letter to the girl : I have received your thoughts and letters , Thank you , And I love you !

After the girl received the boy's reply , I'm happy too , The boy received the love letter . So both boys and girls know each other's feelings , After that, we had a happy exchange ~~

This is the popular version of “ Three handshakes ”, During this period, there were three letters, that is “ Three handshakes ”, To confirm whether the data transmission channels in both directions are normal .

2.3 Why a third handshake ?

In order to prevent the server from opening some useless connections to increase the server overhead and prevent the failed connection request message segments from suddenly transferring to the server , So there's a mistake .

Because network transmission is delayed ( Through network fiber and various intermediate proxy servers ), In the process of transmission , For example, the client initiated SYN=1 Request to create a connection ( The first handshake ).

If the server directly creates the connection and returns the include SYN、ACK and Seq Etc. to the client , This packet is lost due to network transmission , After the loss, the client has not received the data package returned by the server .

The client may have set a timeout , When time is up, the connection creation request is closed . Then reissue the request to create the connection , And the server side doesn't know , If there is no third handshake to tell the server-side client to receive the data transmitted to the server-side ,

The server does not know whether the client has received the information returned by the server .

This process can be understood as :

image.png

This does not give the server a request to create or close the connection port , The server port is always open , Wait until the client reissues the request due to timeout , The server will reopen a port connection . Then the last port on the server that does not receive the request data is always open , In the long term , There are many such ports , It will cause serious waste of server-side overhead .

Another case is the request information sent by the client that has failed , It's transmitted to the server for some reason , The server thinks it is a valid request from the client , Error after receiving .

So we need “ The third handshake ” To confirm the process , Let the client and the server timely detect the connection creation failure caused by network and other problems , In this way, the server port can be closed without waiting .

Or another way to think about it :“ The third handshake ” The client sends data to the server , This data is to tell the server , Did the client receive the server “ The second handshake ” Time passes the past data . If the data sent is “ received ” Information about , After receiving, the server will be set up normally TCP Connect , Otherwise establish TCP The connection fails , The server closes the connection port . This reduces server overhead and errors in receiving invalid requests .

3. TCP Four waves (Four-Way Wavehand)

3.1 Preface

about " Three handshakes " We are familiar with , Because it's relatively simple . however , We don't often hear “ Four waves ”, Even if I have heard it, I may not be able to explain its specific process in detail . Let's go through the details for you , intuitive , Give a complete introduction to “ Four waves ” The process of .

3.2 “ Four waves ” Detailed explanation of

The so-called four waves are TCP Release of connection ( relieve ). The release of the connection must be the active release of one party , The other side passively releases . The following is an illustration of the client initiatively releasing the connection :

image.png Active release of the connection before the end of the client wave ESTABLISHED Stage . Later on “ Four waves ”:

(1) First, the client wants to release the connection , Send a segment... To the server TCP message , among :

  • The tag bit is FIN, Express “ Request to release connection “;
  • Serial number is Seq=U; Then the client enters FIN-WAIT-1 Stage , That is, the half closed phase . And stop sending data from client to server , But the client can still receive the data transmitted from the server .
  • Be careful : What is not sent here is the data transmitted during normal connection ( Unacknowledged message ), Not all the data , So the client can still send ACK Confirmation message .

(2) The server receives... From the client TCP After the message , Confirm that the client wants to release the connection , Then the server ends ESTABLISHED Stage , Get into CLOSE-WAIT Stage ( Half closed ) And return to a paragraph TCP message , among :

  • The tag bit is ACK, Express “ Receive a request from the client to release the connection ”;
  • Serial number is Seq=V; The confirmation number is Ack=U+1, On the basis of receiving the client message , Number it Seq It's worth adding 1 As the confirmation number of this message Ack Value ;
  • Then the server starts to prepare to release the connection from the server to the client .
  • The client receives... From the server TCP After the message , Confirmed that the server received the release connection request from the client , Then the client ends FIN-WAIT-1 Stage , Get into FIN-WAIT-2 Stage

front " Two waves " Let the server know that the client wants to release the connection , Also let the client know that the server knows the request to release the connection . therefore , You can confirm that the connection from the client to the server is closed

(3) Since the server sent ACK After confirming the message , after CLOSED-WAIT Stage , Ready to release the connection from the server to the client , Send a paragraph... To the client again TCP message , among :

  • The tag bit is FIN,ACK, Express “ Ready to release the connection ”.
  • Be careful : there ACK It's not to acknowledge the receipt of a server-side message .
  • Serial number is Seq=W; The confirmation number is Ack=U+1; On the basis of receiving the client message , Number it Seq It's worth adding 1 As the confirmation number of this message Ack Value .
  • Then the server ends CLOSE-WAIT Stage , Get into LAST-ACK Stage .
  • And stop sending data from server to client , But the server can still receive the data transmitted from the client .

(4) The client receives... From the server TCP message , Confirm that the server is ready to release the connection , end FIN-WAIT-2 Stage , Get into TIME-WAIT Stage , And send a message to the server , among :

  • The tag bit is ACK, Express “ Receive a signal that the server is ready to release the connection ”.
  • Serial number is Seq=U+1; It means that on the basis of receiving the server-side message , Put its confirmation number Ack The value is the value of the sequence number of this segment of message .
  • The confirmation number is Ack=W+1; It means that on the basis of receiving the server-side message , Number it Seq The value is the value of the confirmation number of this message .
  • Then the client starts at TIME-WAIT Stage wait 2MSL

3.3 Why should the client wait 2MSL Well ? See you later .

The server receives... From the client TCP End after message LAST-ACK Stage , Get into CLOSED Stage . Therefore, it is officially confirmed to close the connection from the server to the client .

Client waiting 2MSL after , end TIME-WAIT Stage , Get into CLOSED Stage , This completes “ Four waves ”.

after “ Two waves ” Let the client know that the server is ready to release the connection , Also let the server know that the client is ready to release the connection . therefore , You can confirm that the connection from the server to the client is closed , This completes “ Four waves ”.

And “ Three waves ” equally , Transmitted between client and server TCP In the message , Confirmation number of both parties Ack And serial number Seq Value , It's all about each other Ack and Seq Calculated on the basis of values , This guarantees TCP Continuity of message transmission , Once there is a party sending out TCP Message lost , Can't go on " wave ", This ensures that " Four waves " The successful completion of .

3.4 “ Four waves ” The popular understanding of

image.png

" First wave ": See people for a long time , The boy found out that the girl had become a nuisance to himself , The last straw , So I decided to break up , Then wrote a letter to tell the girl .

“ Second wave ”: When the girl received the letter , I know that the boy wants to break up with himself , An angry , Scold in the heart : What do you count? , You didn't look like that ! So I immediately wrote a reply to the boy : Break up and break up , Give me some time , I'm going to put your things in order , Give it all back to you ! After the boy received the girl's first letter , I understand that the girl knows that she wants to break up with her . Then wait for the girl to put her things away .

“ Third wave ”: After a few days , The girl tidied up all the things the boy sent , So I wrote to the boy again : I've arranged your things , Take them away , From then on, you and I break up !

“ Fourth wave ”: After the boy received the girl's second letter , I know the girl has packed up , We can officially break up , So I wrote again to tell the girl : I got it! , Go and get it back !

Here both sides have their own insistence . The girl started with the second letter , No reply from the boy within one day , I'll send another letter urging the boy to come and get something ! The boy started with the second letter , If you don't receive another letter from the girl within two days, you will be deemed as , The girl received her second letter ; If I hear from the girl again in two days , I think the girl didn't receive her second letter , I need to write another letter , Wait another two days …..

If both letters can be received normally , It takes at least four letters to break up completely ! This is it. “ Four waves ”.

3.5 Why? “ handshake ” Three times ,“ wave ” But four times ?

TCP The only reason to establish a connection is to " Three handshakes ", Because the second time " handshake " In the process , What the server sends to the client TCP The message is based on SYN And ACK As a sign bit .SYN Is the request connection flag , Indicates that the server agrees to establish a connection ;ACK It's a confirmation message , Tell the client , The server received its request message .

namely SYN Set up connection message and ACK Confirm that the message is received at the same time " handshake " The transmission of , therefore " Three handshakes " No more, no more , Just so that both sides can clearly communicate with each other .

TCP The reason for releasing the connection is “ Four waves ”, Because FIN Release connection message and ACK Confirm that the received message is from the second time and the third time " handshake " Transmission of . Why are connections transmitted together , When releasing the connection, it needs to be transmitted separately ?

When establishing a connection , Passive server end CLOSED Phase in “ handshake ” The stage doesn't require any preparation , Can return directly SYN and ACK message , Start connection . Release connection , Passive server , The connection cannot be released immediately upon receiving the request of the active client to release the connection , Because there is still necessary data to be processed , So the server returns first ACK Acknowledge receipt of message , after CLOSE-WAIT After the stage is ready to release the connection , Before returning FIN Release connection message .

So it is “ Three handshakes ”,“ Four waves ”.

3.6 Why is the client in TIME-WAIT Stage to wait 2MSL?

In order to confirm whether the server receives the client's sending ACK Confirmation message

When the client sends the last ACK When confirming the message , It is not certain that the server can receive the message . So the client is sending ACK After confirming the message , A duration of 2MSL The timer of .MSL refer to Maximum Segment Lifetime: a section TCP The maximum life cycle of a message during transmission .2MSL That is to say, the server sends it as FIN Messages and messages sent by clients ACK The maximum length of time that a confirmation message can remain valid .

The server is at 1MSL No... From the client was received in ACK Confirmation message , It will send... To the client again FIN message ;

If the client is in 2MSL Inside , Again, I received... From the server FIN message , It indicates that the server side has not received the... Sent by the client due to various reasons ACK Confirmation message . The client sends... To the server again ACK Confirmation message , Timer reset , restart 2MSL Time of ; Otherwise, the client is 2MSL No more... From the server FIN message , It indicates that the server has received normally ACK Confirmation message , The client can access CLOSED Stage , complete “ Four waves ”.

therefore , The client has to go through 2SML Of TIME-WAIT Stage ; That's why the client enters later than the server CLOSED The reason for the stage

copyright notice
author[conner ],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/04/202204291303270338.html

Random recommended