当前位置:网站首页>TCP's three handshakes and four waves
TCP's three handshakes and four waves
2022-07-06 17:03:00 【Society, you Lei brother, life is hard, don't bend down】
One TCP Message format

TCP The source port number and destination port number in the header are the same IP Source in datagram IP And purpose IP Only one TCP Connect .TCP Before sending data to each other , The connection here means : Both parties need to keep each other's information within ( for example :IP,Port…)
Serial number seq: Occupy 4 Bytes , Used to mark the order of data segments ,TCP Put a sequence number on all the data bytes sent in the connection , The number of the first byte is randomly generated locally ; After numbering the bytes , Each segment is assigned a sequence number ; Serial number seq It is the data number of the first byte in this message segment .
Confirmation no. ack: Occupy 4 Bytes , Expect to receive the sequence number of the first data byte of the next message segment of the other party ; The serial number indicates the number of the first byte of data carried by the message segment ; The confirmation number refers to the number of the next byte expected to be received ; So the number of the last byte of the current message segment +1 This is the confirmation number .
confirm ACK: Occupy 1 position , Only when the ACK=1 when , The confirmation number field is valid .ACK=0 when , Invalid confirmation number
Sync SYN: Used to synchronize the serial number when the connection is established . When SYN=1,ACK=0 It means : This is a connection request segment . If you agree to connect , In the response segment SYN=1,ACK=1. therefore ,SYN=1 Indicates that this is a connection request , Or connect to receive messages .SYN This flag is only in TCP It's only set when building production connection 1, After handshake SYN The flag bit is set 0.
End FIN: Used to release a connection .FIN=1 Express : The data of the sender of this message segment has been sent , And asks to release the transport connection
Two Three handshakes
First The last typical picture of three handshakes

The first handshake : When establishing a connection , The client sends syn package (syn=1) To the server , And enter SYN_SENT state , Wait for server to confirm ;
The second handshake : Server to client , The client knows to send 、 Reception is normal , Server receive 、 Send normal .ACK=1,ack=x+1,SYN=1,seq=y
The third handshake : Client receives server's SYN+ACK package , Send confirmation package to server ACK(ack=y+1), This package has been sent , Client and server access ESTABLISHED(TCP Successful connection ) state , Complete three handshakes .
So far, three handshakes also announced Perfect end
3、 ... and Four waves
Let's present the schematic diagram first 
First wave : The client process sends the connection release message , And stop sending data . Release data message header ,FIN=1, Its serial number is seq=u( It is equal to the sequence number of the last byte of the previously transmitted data plus 1), here , Client access FIN-WAIT-1( Stop waiting 1) state . TCP Regulations ,FIN Even if the message segment does not carry data , Also need to consume a serial number .
Second wave : The server receives the connection release message , Send confirmation message ,ACK=1,ack=u+1, And bring your own serial number seq=v, here , The server enters CLOSE-WAIT( Turn off waiting ) state .TCP The server informs the high-level application process , The client is released in the direction of the server , It's half closed , That is, the client has no data to send , But if the server sends data , The client still has to accept . It's going to last a while , That is the whole CLOSE-WAIT The duration of the State . After the client receives the confirmation request from the server , here , The client enters FIN-WAIT-2( Stop waiting 2) state , Wait for the server to send the connection release message ( Before that, you need to accept the last data sent by the server ).
Third wave : After the server sends the last data , Send the connection release message to the client ,FIN=1,ack=u+1, Because it's half closed , The server probably sent some more data , Suppose the serial number at this time is seq=w, here , The server is in LAST-ACK( Final confirmation ) state , Wait for the client to confirm .
Fourth wave : After the client receives the connection release message from the server , Confirmation must be sent ,ACK=1,ack=w+1, And my serial number is seq=u+1, here , The client enters TIME-WAIT( Time waits ) state . Note that this time TCP The connection has not been released , Must go through 2MSL( Maximum segment life ) After , When the client cancels the corresponding TCP after , Only enter CLOSED state . As long as the server receives the confirmation from the client , Enter immediately CLOSED state . Again , revoke TCB after , It's the end of this TCP Connect . You can see , End of server TCP The connection time is earlier than the client .
Interview questions
1 Why three handshakes when connecting , It's four handshakes when it's closed ?
answer : Because when Server End receipt Client Terminal SYN After connecting the request message , It can be sent directly SYN+ACK message . among ACK Messages are used to answer ,SYN Messages are used to synchronize . But when you close the connection , When Server End receipt FIN When the message , The server may not be shut down immediately , So you can only reply one first ACK message , tell Client End ,“ You sent it FIN I received the message ”. Only when I Server All messages are sent , I can send FIN message , So we can't send . So it takes four steps to shake hands .
2 Why? TIME_WAIT Status needs to pass 2MSL( Maximum segment lifetime ) To return to CLOSE state ?
answer : stay Client Send the last ACK reply , But it's time to ACK May be lost .Server If not received ACK, Will be sent repeatedly FIN fragment . therefore Client Can't close now , It has to confirm Server Received this ACK.Client Will send out ACK Then go to TIME_WAIT state .Client A timer will be set , wait for 2MSL Time for . If it is received again within that time FIN, that Client Will be reissued ACK And wait again 2MSL. So-called 2MSL It's two times. MSL(Maximum Segment Lifetime).MSL Refers to the maximum lifetime of a segment in the network ,2MSL It's the maximum time required for a send and a reply . If until 2MSL,Client Didn't receive it again FIN, that Client infer ACK Has been successfully received , End TCP Connect .
3 Why not connect with two handshakes ?
Now suppose there are two computers A and B Communicate with each other , If you shake hands twice, there is a problem , hypothesis A and B Consistent initial column number , but B Can not know A Have you received your own SYN, If this SYN The loss of , be A and B The initial serial number of will not be agreed , Thus, the reliable transmission of data cannot be guaranteed .
边栏推荐
- 程序员定位解决问题方法论
- Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance
- TypeScript基本操作
- 唯有学C不负众望 TOP3 Demo练习
- koa中间件
- MySQL optimization notes
- 7-10 punch in strategy
- [graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
- ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021
- Redis standalone startup
猜你喜欢

Mongodb learning notes

Solr standalone installation

汇编语言寻址方式

Yao BanZhi and his team came together, and the competition experts gathered together. What fairy programming competition is this?

Solr word segmentation analysis

字节跳动技术新人培训全记录:校招萌新成长指南

The most lost road I have ever walked through is the brain circuit of ByteDance programmers
![[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.](/img/ef/2072aac5f85c7daf39174784dec7ee.jpg)
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.

数据传送指令

Error occurred during initialization of VM Could not reserve enough space for object heap
随机推荐
Cartesian tree (modified)
MySQL digital function
我走过最迷的路,是字节跳动程序员的脑回路
在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
唯有学C不负众望 TOP2 p1变量
Only learning C can live up to expectations top5 S1E8 | S1E9: characters and strings & arithmetic operators
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
服务器端渲染(SSR)和客户端渲染(CSR)的区别
Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance
唯有学C不负众望 TOP5 S1E8|S1E9:字符和字符串&&算术运算符
7-8 likes (need to continue to improve)
Shell_ 04_ Shell script
When it comes to Google i/o, this is how ByteDance is applied to flutter
ByteDance technical Interviewer: what kind of candidate do I want to pick most
Shell_ 02_ Text three swordsman
IDEA断点调试技巧,多张动图包教包会。
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
Activiti目录(三)部署流程、发起流程
~75 background
The 116 students spent three days reproducing the ByteDance internal real technology project