当前位置:网站首页>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 .
边栏推荐
- TypeScript基本操作
- 我走過最迷的路,是字節跳動程序員的腦回路
- 汇编语言段定义
- redux使用说明
- Basic knowledge of assembly language
- ByteDance technical Interviewer: what kind of candidate do I want to pick most
- Shell_ 05_ operator
- The "advertising maniacs" in this group of programmers turned Tiktok advertisements into ar games
- Log4j2 major vulnerabilities and Solutions
- string. How to choose h and string and CString
猜你喜欢
Shell_ 06_ Judgment and circulation
Alibaba cloud server builds SVN version Library
Eureka single machine construction
Solr standalone installation
谢邀,人在工区,刚交代码,在下字节跳动实习生
was unable to send heartbeat
Error occurred during initialization of VM Could not reserve enough space for object heap
J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets
Usage of insert() in vector
逻辑运算指令
随机推荐
Shell_ 02_ Text three swordsman
唯有学C不负众望 TOP5 S1E8|S1E9:字符和字符串&&算术运算符
Jedis
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
汇编课后作业
MySQL日期函数
Activiti directory (III) deployment process and initiation process
Redis standalone startup
Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
MySQL date function
字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
~69 other ways to use icon fonts
redux使用说明
~74 JD top navigation bar exercise
字节跳动2022校招研发提前批宣讲会,同学们最关心的10个问题
ByteDance technical Interviewer: what kind of candidate do I want to pick most
README. txt
The "advertising maniacs" in this group of programmers turned Tiktok advertisements into ar games