当前位置:网站首页>The illustration shows three handshakes and four waves. Xiaobai can understand them
The illustration shows three handshakes and four waves. Xiaobai can understand them
2022-06-23 06:58:00 【Shu Yang】
TCP Provisions of the agreement , A connection must be established before transmitting data ( Three handshakes ), Release the connection after data transfer ( Four waves ).
TCP The header of the message segment is shown in the following figure , Before introducing three handshakes and four waves , Need to know TCP Some fields in the header of the message segment , Because Xiaobai can understand the series , Just a few things you must know :

seq Serial number :seq As a sender , The... Of the data sent 1 Number of bytes .
ack Confirmation no. :ack As a receiver , The... Of the next message segment expected to be received 1 Number of bytes .
What do the above two fields mean , for instance ,A to B Send a paragraph TCP message , In this message seq=100, Express A towards B The data sent is 1 The sequence number of bytes is 100, If the data length of this message is 200 Bytes , that B After receiving this message correctly , Want to A Return a message , In this message ack=100+200+1=301, Express B The sequence number of the first byte of the next message segment expected to be received is 301.
If B Reply message 2 in ack<301, The assumption is 201, This indicates that from the serial number 201~300 The data of B None of them received ! Now A You know that you need to resend the lost data , This is it. TCP One of the ways to ensure reliable transmission .
Then you must have questions , A message contains both seq And then there is ack, Is it the sender or the receiver ? answer :TCP It's full duplex , Can send and receive at the same time , Both sides of the communication can act as the sender and the receiver , therefore ,A In the B The message that sends the data also carries the reply B Of ack, That is to say seq and ack It works at the same time . Of course , It can also be as mentioned above , One is the sender and the other is the receiver , Just make one of the two fields valid .SYN Sync bit :SYN=1 It means that this is a message requesting or accepting connection .
ACK Confirmation bit : Only if the confirmation bit ACK=1 The confirmation number ack It works , When ACK=0 when ack Invalid .
I said before. SYN=1 It means that this is a message requesting or accepting connection . When SYN=1 And ACK=0 It indicates that this is a message requesting connection , When SYN=1 And ACK=1 It means that this is a message that accepts the connection , Don't ask why , Under the agreement .FIN Stop bit : When FIN=1, Indicates that the sender's data of this message segment has been sent , Request to release connection .
Three handshakes
For simplicity , The picture only shows TCP Part of the premiere .
The first handshake :
The second handshake :
The third handshake :
The above figure is the following classic figure :
Why three handshakes , Not twice ?
Answer one :
If this happens now :A The first connection request message sent , But this connection request message has been stuck in the network for a long time , The delay is long until the connection is released B. This was originally a message that had long failed , but B I don't know ,B I mistakenly thought A Another connection request was made , So I happily went to A A confirmation message was replied , Agree to establish a connection .
The problem arises , If we now use two handshakes , So the above situation ,B The connection has been established after the reply . and A It doesn't have to be the same B Establishing a connection , Of course, they will not pay attention to B Confirmation message sent , Not to mention B send data .B I don't know these ,B Think the connection has been established , Waiting in silence A Send data , This is a waste B Resources for .
So a third handshake is necessary .
Answer two :
You can also think like this : After the first handshake ,B got it —— I can receive A Message sent ( I can take it ). After the second handshake ,A got it —— I sent messages from the past B To receive , And I can also receive B Message sent ( I can send and receive ). After the third handshake ,B got it —— I sent it to A The news of A To receive ( I can send ). After three handshakes , Communication forms a closed loop , Both sides know that they can send and receive , It's ready to transfer data .
If there is no third handshake ,B In fact, I don't know whether I can successfully send messages , because A I didn't reply to the message I sent , You may not be able to send messages successfully , So this third handshake is needed .
TCP Can I carry data during connection establishment , Why? ?
answer : The message segments of the first handshake and the second handshake cannot carry data , The message segment of the third handshake can carry data . Why? ?TCP Under the agreement .
Four waves
TCP It's full duplex , Both sides of communication can send and receive data , We can take this as an example , Sending data means , Receiving data is listening , To release the connection completely , Both sides should not send ( say ) Not accept ( listen ).
First wave :A No more sending data .
Second wave :B No longer receiving data .
here ,TCP Connection entered Half closed , namely A There is no data to send ,B I won't accept it , but B You can still send data ,A Can still receive data .
In the half closed state B Possible direction A And sent a lot of data , When B When there is no data to send , Suppose the current serial number has arrived w, namely seq=w, At this time B Send connection release message ,B The confirmation number sent last time must be repeated ack=u+1.
Third wave ,B No more sending data .
A Upon receipt of B After the connection release message of , This must be confirmed , After sending the confirmation message ,A Will enter the TIME-WAIT( Time waiting state ). Please note that ,A After sending the confirmation message TCP The connection was not immediately released , It has to take time for the timer (TIME-WAIT timer) Time set 2MSL(2 Maximum message segment life ) after ,A To get into CLOSED state .
Fourth wave ,A No longer receiving data .

The above figure is the following classic figure :
Why? A stay TIME-WAIT The state must wait 2MSL Time for ?
First of all , In order to ensure A The last one sent ACK Message segments can reach B.
second , Prevent the invalid connection request message segment from appearing in this connection .
Reference books 《 computer network 》 Edited by Xie Xiren .
边栏推荐
猜你喜欢

MySQL basic query
![[bull Chinese document] queue package used to process distributed jobs and messages in nodejs](/img/f9/1bd79d3754c1b4d1b114d02283f95e.png)
[bull Chinese document] queue package used to process distributed jobs and messages in nodejs

Easy EDA learning notes 09 esp32-wroom-32e module esp32-devkitc-v4 development board one click download circuit

Badly placed ()‘s 问题

【STL】顺序容器之deque用法总结

小白投资理财必看:图解基金买入与卖出规则

Sword finger offer 42 Maximum sum of successive subarrays

Summary of qvariant use in QT

Children's programming for comprehensively cultivating students' mental thinking

mingw-w64、msys和ffmpeg的配置与编译
随机推荐
反鸡汤致辞
C language operator priority formula
[project training] change of linear arrow
cmder
Add IPAD control function into shairplay
Summary of qvariant use in QT
994. 腐烂的橘子-非递归法
ssm + ftp +ueditor
DQL、DML、DDL、DCL的概念与区别
【shell】Tree命令
QT method of compiling projects using multithreading
Centos7 MySQL records
MySQL redo log redo log
Drawing and resetting of mars3d point, line and surface
【项目实训】线形组件的细节
Chrome删除重复书签
Measurement principle and thickness measurement mode of spectral confocal
Media industry under the epidemic situation, small program ecology driven digital transformation exploration
Mongodb record
ssm + ftp +ueditor