当前位置:网站首页>Second handshake?? Three waves??
Second handshake?? Three waves??
2022-07-29 10:14:00 【Walnut is surnamed Hu, and butterfly is also surnamed Hu】
When we learn 《 computer network 》 At the time of this course , I will definitely learn TCP This classic transport layer protocol ,TCP There is a feature called Connection management , This feature leads to two concepts ,TCP Three handshakes and four waves . Why three times , And why is it four times this problem has troubled me for a long time , Today I will explain this reason in detail .
On this question , Most articles will quote 《 computer network 》 A sentence in it .
Prevent invalid connection requests from being sent to the server , So there's a mistake
Actually, that's too broad , Can't really understand .
TCP What is the communication process like ?
Send messages to each other , This is a very easy thing to understand .
Such as this :
At this time, we need to introduce a concept , be called TCP The buffer :
TCP The buffer of can be understood as a very large array :

When there is data transmission in our application layer , Writing data to the transport layer through the system call interface is written in TCP In the buffer of the Protocol ,TCP Meeting malloc Two segments of memory space . The role of having a buffer is obvious :
- Improve the efficiency of the application layer
- Only OS,TCP The protocol can know the network , Even the status details of the other party , So only TCP agreement , Can handle how to send , When to send , How many hair , What to do if something goes wrong , Because the buffer exists , So we can achieve application layer and TCP Decoupling
my TCP There is a lot of data in the buffer , Some data has been sent , Some have not been sent , So where should we develop and send the connection this time ? This is not necessarily from 0 Location ( Subscript ) At the beginning , therefore client End sum server The client needs to know where the buffer of the other party comes from ( The array subscript ) Develop and send data .
- For reliable data transmission , TCP The parties to the agreement , You have to maintain a serial number , To identify the packets sent , What has been received by the other party . The process of three handshakes is that the communication parties inform each other of the starting value of the serial number , And confirm that the other party has received the start value of the serial number
- If it's just two handshakes , At most, only the starting serial number of the connection initiator can be confirmed , The serial number chosen by the other party is not confirmed
So let's see TCP The whole process of communication :

You can see the process of confirming the serial number by both parties .
Why? TCP You need to shake hands ? Can you stop shaking hands ?
If the contrast UDP Agreement can be found ,UDP Handshake is not required in the Protocol , Here we need to draw TCP and UDP The characteristics of the Protocol , A key feature is ,TCP The agreement is a reliable agreement ,UDP An agreement is an unreliable agreement .
Reliable agreement means : The data received by the receiver is complete , Orderly , Error free .
Unreliability means : The data received by the receiver may be partially lost , Sequence is not guaranteed .
TCP Protocol in order to achieve reliability , Both sides of the communication need to know whether the data they send has been received by the receiver , If not received , You need to resend . In order to achieve this requirement , About to lead out Serial number and Confirm the serial number .
The sender is sending a packet ( Assume the size is 10 byte) when , At the same time, send a serial number ( Assuming that 500), After the receiver receives the packet , You can reply with a confirmation number (510 = 500 + 10) Tell sender “ I have received your packet , You can send the next packet , The 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 .
So there is the process of the above figure , This explains why 3 Time , instead of 2 Time .

Someone may be interested in the above picture TCP There are questions about the data of the communication process .
- Why three handshakes, the last handshake , Reply in the diagram above seq = x+1 .
Take a look at a passage in the big black book :
What is said here is very complicated , It's actually : my ACK The serial number inside is the next one I need to get .

So why four waves ?
Can't you wave your hand like three handshakes ?
The answer is no , as a result of :TCP Agreement is Full duplex .client and server Both ends need to transmit and process data . I client End request to close the link , send out FIN, from OS The kernel rules , send out FIN It will echo immediately after ACK To show that I have received this message , But my server The client may be processing data at this time , So you can't close it immediately , You can only send it after the matter is handled FIN to client, It means I'm done ,client Echo after receiving ACK To indicate receipt of , then server close ,client stay 2MSL After that, it is also closed .
If server There is nothing to deal with , that FIN and ACK Will be merged and sent , That is the so-called three waves .
in other words , Compared to three handshakes , Four waves, one more I'll call you back when I finish my work The process of .
It's like I go out to play with walnuts :
I : walnut , Let's go out and play !
walnut : All right ! But I have something , I'll call you after I finish the work .
walnut : I'm done , Let's go !
I : well !
Why? client etc. 2MSL Just close ?
This is also the guarantee of reliability , If I send it ACK I'll wait for the packet to be sent again , What if I quit the second ?
边栏推荐
- After eating Alibaba's core notes of highly concurrent programming, the backhand rose 5K
- Dimensionality reduction and mathematical modeling after reading blog!
- The purpose of DDD to divide domains, sub domains, core domains, and support domains
- 待人宽容大度
- Unity3d空包打apk报错汇总
- shell编程之sed,正则表达式
- PDF处理还收费?不可能
- Shell笔记(超级完整)
- 那句话的作用
- Notes for Resume Writing
猜你喜欢

Knowledge points of common interview questions: distributed lock
![[wechat applet] interface generates customized homepage QR code](/img/9b/cccdb8ff6db61518402a27b94d0196.png)
[wechat applet] interface generates customized homepage QR code

Vector implementation

基于SSM实现高校后勤报修系统

关系型数据库之MySQL8——由内而外的深化全面学习
![[paper reading] i-bert: integer only Bert quantification](/img/2e/4f574b266ec6fc88ffa5dab56f2b8d.png)
[paper reading] i-bert: integer only Bert quantification

英特尔联合Datawhale,发布学习项目!

不堆概念、换个角度聊多线程并发编程

Print out the "hourglass" and the remaining number according to the given number of characters and characters

二次握手??三次挥手??
随机推荐
The function of that sentence
TMS320C6000_ Tms320f28035 Chinese data manual
How to customize the opportunity closing form in dynamics 365online
Docker安装Redis、配置及远程连接
造型科幻、标配6安全气囊,风行·游艇11.99万起售
【AAAI】用于交通流预测的基于注意力的时空图卷积网络
全面、详细的SQL学习指南(MySQL方向)
Method of cocos2d-x sprite moving
Function - (C travel notes)
还是有机会的
Sed, regular expression of shell programming
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
SAP Fiori @OData. Analysis of the working principle of publish annotation
SkiaSharp 之 WPF 自绘 弹动小球(案例版)
The maximum length of VARCHAR2 type in Oracle is_ Oracle modify field length SQL
【论文阅读】Q-BERT: Hessian Based Ultra Low Precision Quantization of BERT
Skiasharp's WPF self drawn bouncing ball (case version)
Linear regression of machine learning (least square handwriting +sklearn Implementation)
A Zuo's realm
How to integrate Google APIs with Google's application system (3) -- call the restful service of Google discovery API