当前位置:网站首页>Six pictures show you why TCP shakes three times?
Six pictures show you why TCP shakes three times?
2022-07-24 08:56:00 【Xiao Liu xuezhuo】
Three handshakes

picture

picture
Two handshakes ( situation 1)

picture
Two handshakes ( situation 2)

picture
OK, Let's answer this question seriously , We need to find out the problem , First of all, understand TCP How to ensure reliable transmission .

picture
PS:TCP Agreement , The end that initiates the request is called 『 client 』, The end of the passive connection is called 『 Server side 』. Whether it's the client or the server ,TCP Data can be sent and received after the connection is established .
At first , The server And client are both CLOSED state . Before communication begins , Both sides have to create their own transmission control blocks (TCB). The server is created TCB After entering LISTEN state , At this time, you are ready to receive the connection request from the client .
The first handshake
The client sends the connection request message segment to the server . In the header of the segment SYN=1,ACK=0,seq=x. After the request is sent , The client enters SYN-SENT state .
- PS1:SYN=1,ACK=0 Indicates that the segment is a connection request message .
- PS2:x For this time TCP The initial sequence number of the byte stream of communication .TCP Regulations :SYN=1 The message segment of cannot have data part , But to consume a sequence number .
The second handshake
After the server receives the connection request message segment , If you agree to connect , A response will be sent :SYN=1,ACK=1,seq=y,ack=x+1. After the response is sent, it will enter SYN-RCVD state .
- PS1:SYN=1,ACK=1 Indicates that the message segment is the reply message of connection agreement .
- PS2:seq=y When the server is the sender , Send the initial sequence number of the byte stream .
- PS3:ack=x+1 Indicates that the server wants to send the next datagram sequence number from x+1 Start byte .
The third handshake
When the client receives the reply of the connection consent , It also sends an acknowledgement message segment to the server , Express : The connection consent response from the server has been successfully received . The header of the message segment is :ACK=1,seq=x+1,ack=y+1. After the client sends this message segment, it will enter ESTABLISHED state , After receiving this response, the server will enter ESTABLISHED state , At this point, the connection is established !
Why does it take three handshakes to establish a connection , Not two handshakes ?
To prevent the invalid connection request message segment from being received by the server , So there's a mistake .
PS: Invalid connection requests : If the connection request sent by the client to the server is lost , The client will send the connection request again after waiting for a response , here , The last connection request was 『 Invalid 』.
If a connection is established, only two handshakes are needed , The client doesn't change much , You still need to get the response from the server before entering ESTABLISHED state , The server enters after receiving the connection request ESTABLISHED state .
At this point, if the network is congested , The connection request sent by the client fails to reach the server , The client will time out to resend the request , If the server receives and confirms the response correctly , Double convenient start communication , Release the connection when communication is over . here , If the invalid connection request reaches the server , Because there were only two handshakes , When the server receives the request, it will enter ESTABLISHED state , Waiting to send data or actively sending data . But the client has already entered CLOSED state , The server will always wait , This wastes the connection resources on the server side .
TCP Four waves

picture
TCP The release of the connection takes four steps , So called 『 Four waves 』.
We know ,TCP The connection is two-way , So in four waves , The first two waves are used to disconnect in one direction , The last two waves are used to disconnect the connection in the other direction .
First wave
if A Think data transmission is complete , Then it needs to be directed to B Send connection release request . The request has only header , The main parameters carried in the head are :FIN=1,seq=u. here ,A Will enter the FIN-WAIT-1 state .
- PS1:FIN=1 Indicates that the segment is a connection release request .
- PS2:seq=u,u-1 yes A towards B The sequence number of the last byte sent .
Second wave
B After receiving the connection release request , The corresponding application will be notified , Tell it A towards B The connection in this direction has been released . here B Get into CLOSE-WAIT state , And to A Send a connection release response , Its header contains :ACK=1,seq=v,ack=u+1.
- PS1:ACK=1: except TCP Outside the connection request segment ,TCP All datagrams in the process of communication ACK All for 1, To answer .
- PS2:seq=v,v-1 yes B towards A The sequence number of the last byte sent .
- PS3:ack=u+1 Wish to receive from u+1 A message segment starting with bytes , And has successfully received the front u Bytes .
A Received the response , Get into FIN-WAIT-2 state , wait for B Send connection release request .
After the second wave ,A To B The connection in the direction has been released ,B No more data will be received ,A No more data will be sent . but B To A The connection of directions still exists ,B You can go on to A send data .
Third wave
When B towards A After sending all the data , towards A Send connection release request , Request header :FIN=1,ACK=1,seq=w,ack=u+1.B Then enter LAST-ACK state .
Fourth wave
A After receiving the release request , towards B Send a confirmation response , here A Get into TIME-WAIT state . This state will last 2MSL Time , If there is no B If you want to resend the request , To get into CLOSED state , revoke TCB. When B After receiving the confirmation reply , And then I went into CLOSED state , revoke TCB.
Why? A Enter first TIME-WAIT state , wait for 2MSL It's time to enter CLOSED state
In order to ensure B To receive A The confirmation response . if A After sending the confirmation response, directly enter CLOSED state , Then if the reply is lost ,B After the timeout, the connection release request will be sent again , But this time A It's closed , There will be no response , therefore B Never shut down properly .
边栏推荐
- Learn the rxjs operator
- Wargames NATAS (11-15) problem solving essay
- 使用Go语言开发eBPF程序
- 安装软件时提示【An error occurred while trying to create a file in the destination directory: 拒绝访问】的解决方法
- Wildcards in MySQL like statements: percent, underscore, and escape
- Recursive performance test
- How should tiktok shop cooperate with live broadcast in the background?
- Meta tags let search engines search your website
- Typora prompt [this beta version of typora is expired, please download and install a new version]
- What is tiktok creator fund and how to withdraw it?
猜你喜欢

Xiaobai learns oauth2

C语言练习题目+答案:

How to integrate and use log4net logging plug-in in vs2019 class library

RPC中实现提供者信息变化后通知消费者

面试官:哥们Go语言的读写锁了解多少?

Super complete summary: how to operate files in go language

Tiktok's "online celebrity" was poached by Amazon and broadcast on Amazon live platform

Meta tags let search engines search your website

【翻译】使用gRPC和REST的微服务架构中的集成挑战

How to import CAD files into the map new earth and accurately stack them with the image terrain tilt model
随机推荐
Grpc learning notes
C语言练习题目+答案:
Learn the rxjs operator
What is the "age limit" on tiktok and how to solve it?
Four data interaction modes of go grpc
Super complete summary: how to operate files in go language
【一起上水硕系列】EE feedback 详解
Wildcards in MySQL like statements: percent, underscore, and escape
[Sheung Shui Shuo series] EE feedback details
Online lover
Unity解决Package Manager“You seem to be offline”
Relationship between fork and pipeline
利用opencv 做一个简单的人脸识别
The detailed process of building discuz forum is easy to understand
【一起上水硕系列】June总结+no 焦虑+July计划+如何考试+如何提升
[Shangshui Shuo series together] June summary +no anxiety +july plan + how to test + how to improve
Discuz论坛搭建详细过程,一看就懂
使用分区的优点
POI operation excel collation
What is tiktok creator fund and how to withdraw it?