当前位置:网站首页>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 ?




边栏推荐
- Property or method “approval1“ is not defined on the instance but referenced during render
- 编译丨迅为STM32P157开发板编译U-Boot源码
- List becomes vector list becomes vector list vector
- [C language] detailed explanation of threads - multi threads for collaborative operation
- ROC-RK3566-PC使用10.1寸IPS触摸屏显示
- [200 opencv routines] 215 Drawing approximate ellipse based on polyline
- 【网易云信】播放demo构建:无法将参数 1 从“AsyncModalRunner *”转换为“std::nullptr_t”**
- 【C语言】详解线程 — 多线程进行协同运算
- 【Proteus仿真】Arduino UNO利用74LS148扩展中断
- Several cross end development artifacts
猜你喜欢

splitting.js密码显示隐藏js特效

Parker variable displacement piston pump pv092r1k1t1nmmc

Compile - compile for itop4412 development board makefile

svg实现的订票UI效果

Acwing game 57

开发那些事儿:Linux系统中如何安装离线版本MySQL?

面试突击60:什么情况会导致 MySQL 索引失效?

Exercise book of introduction to database system

浅析搭建高速公路视频监控平台的建设方案及必要性

【OpenCV 例程200篇】215. 基于多段线绘制近似椭圆
随机推荐
解决方法:STM32使用cJSON解析数据失败
How to connect the Internet Reading Notes - Summary
[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
Daily question brushing record (IX)
3D chart effectively improves the level of large data screen
小程序容器与物联网结合的方式
基于SSM实现毕业设计管理系统
【JVM】一文带你了解JVM中的垃圾回收机制(GC)——内含图解
Cesium-1.72 learning (model attitude control)
定时任务删除指定时间的的数据
每日刷题记录 (九)
编译丨迅为STM32P157开发板编译U-Boot源码
数据分析新动力——国内首款开源一体化实时HTAP数据库石原子StoneDB发布
Property or method “approval1“ is not defined on the instance but referenced during render
平面相交与平面方程
Cesium-1.72 learning (China national boundary)
Exercise book of introduction to database system
水平视觉错误效果js特效代码
Supplementary
【OpenCV 例程200篇】215. 基于多段线绘制近似椭圆