TCP Why shake hands three times instead of twice （ Correct interpretation ）
<> Reference articles
Why do we need a 3-way handshake? Why not just 2-way
<> Misinterpretation of most Weblogs
The first thing to declare is , Baidu search to most of the network blog on the answer to this question are not clear or accurate . Most of the blogs that talk about it will cite it 《 computer network 》 Content of ：
* Prevent invalid connection requests from being sent to the server , This leads to errors
Unfortunately , This explanation is not accurate , TCP The reason for three handshakes is very simple , It's not as foggy as most blogs describe .
Here is the conclusion ：
* In order to achieve reliable data transmission , TCP Communication parties of the agreement , Must maintain a serial number , To identify the packets sent out , What have been received by the other party .
The process of three handshakes is that the communication parties inform each other of the starting value of serial number , And confirm that the other party has received the starting value of serial number
* If it's just two handshakes , At most, only the starting sequence number of the connection initiator can be confirmed , The serial number selected by the other party is not confirmed
<> Prerequisite knowledge
<>TCP Communication process
TCP Communication flow of
Each arrow in the image above represents one time TCP Sending packets
* It should be noted that , In the picture above ACK = x +1 It's easy to make people mistakenly think that the ACK The data value of the field is filled in y+1 . ACK = x+1
The actual meaning of ：
* TCP Packaged ACK Marker bit （1 bit） Is set to 1
* TCP Confirmation number of the package （acknowledgement number ） The value of is x+1
* allied , TCP In the packet SYN Marker bit , Also easy with serial numbers （sequence number） confusion , This needs the attention of readers
TCP Packet structure diagram
<> Why? TCP You need to shake hands
Why TCP Three handshakes are needed , Not twice before , The first question to be answered is :
* Why do you need to shake hands , Can we not shake hands ?
If readers compare it UDP Communication process and TCP Communication flow of , We can find out , stay UDP In the agreement , There is no handshake operation .
This leads to this TCP And UDP One of the basic differences between , TCP It's a reliable communication protocol , and UDP It's an unreliable communication protocol .
* TCP The meaning of reliability ： The data received by the receiver is complete , Order , Error free .
* UDP Meaning of unreliability ： The data received by the receiver may be partially lost , Sequence is not necessarily guaranteed .
UDP and TCP The protocols are all based on the same Internet infrastructure , And all based on IP Protocol Implementation , In the Internet infrastructure, packet loss occurs in the process of sending packets , Why? TCP
Reliable transmission can be achieved , and UDP no way ?
TCP In order to achieve reliable transmission , Both sides of the communication need to judge whether the packets they have sent are received by the receiver , If you don't get it , You need to resend it . In order to achieve this requirement , It's very natural
Serial number （sequence number） and Confirmation number （acknowledgement number） Use of .
The sender is sending packets （ Suppose the size is 10 byte） Time , At the same time send a serial number ( Suppose it is 500), After receiving the packet , You can reply with a confirmation number （510 =
500 + 10） Tell the sender “ I've received your packet , You can send the next packet , Serial number from 510 start ” .
In this way, the sender can know which data has been received , What data was not received , Need to resend .
<> Why do you need to shake hands three times , Not twice
As described above , In order to achieve reliable transmission , The sender and receiver always need to be synchronized ( SYNchronize ) Serial number . It should be noted that , The serial number is not from 0 Starting ,
It is an initial sequence number randomly selected by the sender ( Initial Sequence Number, ISN ) start . because TCP It's a two-way communication protocol ,
Both sides of the communication have the ability to send information , And receive the response . therefore , Both sides of communication need to generate an initial sequence number randomly , And tell each other the starting value .
therefore , This process becomes the following .
The flow chart below describes the same as above , But it's clearer TCP Packet flag bit , And the naming source of the data field .
AliceBobSYN =1 , seq = xSYNchronize withmy InitialSequence Numberof xSYN =1,
ACK = 1, seq = y , ack = x+1I received yourISN, IACKnowledge thatI am ready for
[x+1]SYNchronize withmy InitialSequence Numberof yACK =1 , seq = x+1, ack = y+1
I received yoursyn, IACKnowledge thatI am ready for[y+1]AliceBob