当前位置:网站首页>Six pictures show you why TCP has three handshakes?
Six pictures show you why TCP has three handshakes?
2022-06-30 17:22:00 【Xinyi 2002】
Three handshakes


Two handshakes ( situation 1)

Two handshakes ( situation 2)

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

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 , Both the server and the client are 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

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 .
source :https://mp.weixin.qq.com/s/oKOjjXm6OcdVT_laAeDp6Q
Recommended reading
NO.1
Previous recommendation
Historical articles
9 It's easy to use JSON Processing tools , Greatly improve efficiency !
It's so sweet ! The most comprehensive Pycharm Common shortcut keys !
Pandas Easy for novices 6 A mistake
Python Eight schemes to realize timed tasks , Dry cargo is full.
Share 、 Collection 、 give the thumbs-up 、 I'm looking at the arrangement ?




边栏推荐
- Compile u-boot source code for stm32p157 development board
- 商鼎雲新版來襲 | 收藏夾功能已上線,滿足個人使用需求
- JS from prototype chain to inheritance
- 美国穆格moog伺服阀D661-4577C
- canvas鼠标控制重力js特效
- Data security compliance has brought new problems to the risk control team
- nodejs学习笔记二
- 【C语言】详解线程 — 多线程进行协同运算
- Ningx 1.20.2
- 将 EMQX Cloud 数据通过公网桥接到 AWS IoT
猜你喜欢

parker比例溢流阀RS10R35S4SN1JW

Acwing game 57

Alibaba cloud disk sharing compressed package

Compile - compile for itop4412 development board makefile

Consolidate entry-C basic variables and constants

leetcode:1042. 不邻接植花【随机填入符合要求的 + 后面不会形成矛盾 + set.pop】

基于SSM实现毕业设计管理系统

k线图快速入门必读

Property or method “approval1“ is not defined on the instance but referenced during render

Implementation of graduation project management system based on SSM
随机推荐
Write the simplest small program in C language Hello World
Compile u-boot source code for stm32p157 development board
[demo] write file circularly
Several cross end development artifacts
Exch:完整性检查 Database Integrity Checking
geo 读取单细胞csv表达矩阵 单细胞 改列名 seurat
【OpenCV 例程200篇】215. 基于多段线绘制近似椭圆
Geo read single cell CSV expression matrix single cell column name change Seurat
水平视觉错误效果js特效代码
面试突击60:什么情况会导致 MySQL 索引失效?
redis淘汰策略
MySQL8 NDB Cluster安装部署
Pref usage record
Differential analysis between different groups nichenet for silicosis runs successfully!
Cesium-1.72 learning (camera tracking)
addmodule_ allmerge_ ams_ im
Design of piece counter based on 51 single chip microcomputer
分享 5 大常用的特征选择方法,机器学习入门必看!!!
[untitled] write a student achievement and information management system in C language to realize the operation interface, clear screen display of current operation functions, reading and inputting st
期未课程设计:基于SSM的产品销售管理系统