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




边栏推荐
- 列表变成向量 列表变向量 list vector
- More than 20million videos have been played in the business list! Why is the reform of Agricultural Academies urged repeatedly?
- Login box tricks
- 补充材料 supplementary
- [demo] write file circularly
- addmodule_allmerge_ams_im
- List becomes vector list becomes vector list vector
- How to connect the Internet Reading Notes - Summary
- JS from prototype chain to inheritance
- 商鼎云新版来袭 | 收藏夹功能已上线,满足个人使用需求
猜你喜欢

每日刷题记录 (九)

Internet R & D efficiency practice qunar core field Devops landing practice

数据分析新动力——国内首款开源一体化实时HTAP数据库石原子StoneDB发布

How can you choose to work in the county after graduation?

Required plug-ins for idea

知名互联网房屋租赁服务公司物联网关键业务迁移上云实践

How to connect the Internet Reading Notes - Summary

编译丨迅为iTOP4412开发板Makefile编译

Cesium-1.72 learning (camera tracking)

Write the simplest small program in C language Hello World
随机推荐
高等数学(第七版)同济大学 总习题一 个人解答
k线图精解与实战应用技巧(见位进场)
山西化工园区智能化管控平台建设时间表
编译丨迅为STM32P157开发板编译U-Boot源码
Required plug-ins for idea
Plane intersection and plane equation
5G商用三年,未来创新何去何从?
【C语言】详解线程 — 线程分离函数 pthread_detach
Cesium-1.72 learning (earth rotation)
Sub chain cross technology source level exploration: an overview of xcvm
How to connect the Internet Reading Notes - Summary
differential analysis between different groups nichenet for silicosis成功运行!
idea必用插件
【JVM】类加载相关面试题——类加载过程、双亲委派模型
以往我们认识的产业互联网,只是以消费互联网的替代者的身份出现
pref使用记录
基于SSM实现毕业设计管理系统
MySQL advanced notes
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
商鼎云新版来袭 | 收藏夹功能已上线,满足个人使用需求