当前位置:网站首页>你需要知道的 TCP 三次握手
你需要知道的 TCP 三次握手
2022-07-06 04:46:00 【前端西瓜哥】
TCP,全称 Transmission Control Protocal。从名字可以知道这是一个用于 控制传输 的位于传输层的协议。
TCP 位于 TCP/IP 和 OSI 模型的传输层。我们最常使用的 HTTP 协议,底层通常使用的就是 TCP 协议。
如果要在客户端和服务端创建 TCP 连接,我们需要在开始的时候发送三个请求确认双方的通信能力正常,这三次连接就被称为 TCP 的三次握手。
TCP 的三次握手
下图为 TCP 头部的结构,我们的 TCP 三次握手主要用到其中的标黄的部分。
下面开始讲解这三次握手。
第一次握手
客户端给服务端发送一个 TCP 报文。在 TCP 头部中:
将 SYN 标志位设置为 1;
然后给 seq 赋予一个随机数(假设这个随机数是 x)
所谓标志位,其实就是一个比特的数据,设置为 1 ,其实就类似编程中将一个布尔值设置为 true。
SYN 代表同步(sync)的意思,seq 是序列号(Sequence number)的意思,是一个可以装载数值的地方,我们给它设置为随机数,是用来做凭证(token)的,稍后服务端会使用它。
第二次握手
服务端收到 SYN,知悉客户端希望建立 TCP 连接,并拿到了一个 seq 随机数。
服务端于是向客户端发送 TCP 报文,TCP 头中:
将 ACK 标志位设置为 1,表示已知悉(acknowledge)客户端的连接请求;
ack 的值设置为客户端传过来的 seq 值 + 1(即 x+1);
将 SYN 标志位设置为 1,表示服务端也要连接;
seq 的值设置一个随机数(假设随机数为 y)
服务端通过 ACK 和 ack 来告知客户端已知悉连接请求,并发送自己的 SYN 和 seq 来请求和客户端的连接。
第三次握手
客户端收到 TCP 请求,首先验证 SYC 是否为 1, seq 是否为 x+1。
如果是,说明服务端的这个发送是对应第一次握手的返回。客户端会再发送一个 TCP 请求:
ACK 设置为 1;
ack 设置为服务端 ack 的值 + 1(即 y + 1)
三次握手后,TCP 连接就建立了。
总结
三次握手,简单来说,就是这样的:
客户端 -> 服务端:SYN=1, seq=x (x为随机数)
服务端 -> 客户端:ACK=1, ack=x+1, SYN=1, seq=y (y为随机数)
客户端 -> 服务端:ACK=1, ack=y+1
我是前端西瓜哥,欢迎关注我,掌握更多前端面试题。
边栏推荐
- 关于imx8mp的es8316的芯片调试
- How does computer nail adjust sound
- L'introduction en bourse de MSK Electronics a pris fin: 800 millions de RMB d'actifs de Henan étaient des actionnaires
- [05-1, 05-02, 05-03] network protocol
- 11. Intranet penetration and automatic refresh
- Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
- 比尔·盖茨晒18岁个人简历,48年前期望年薪1.2万美元
- CADD课程学习(8)-- 化合物库虚拟筛选(Virtual Screening)
- CADD course learning (8) -- virtual screening of Compound Library
- 牛顿插值法
猜你喜欢
Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码
[数学建模] 微分方程--捕鱼业的持续发展
程序员在互联网行业的地位 | 每日趣闻
yolov5 tensorrt加速
Easyrecovery靠谱不收费的数据恢复电脑软件
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
Ue5 small knowledge points to enable the setting of lumen
Orm-f & Q object
Basic knowledge and examples of binary tree
CADD course learning (7) -- Simulation of target and small molecule interaction (flexible docking autodock)
随机推荐
Postman管理测试用例
npm命令--安装依赖包--用法/详解
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
SQL注入漏洞(MSSQL注入)
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
ORM aggregate query and native database operation
win10电脑系统里的视频不显示缩略图
I'd like to ask about the current MySQL CDC design. In the full volume phase, if a chunk's binlog backfill phase,
Crazy God said redis notes
Visio draw fan
也算是学习中的小总结
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
MySQL reported an error datetime (0) null
The most detailed and comprehensive update content and all functions of guitar pro 8.0
Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
The web project imported the MySQL driver jar package but failed to load it into the driver
A blog to achieve embedded entry
Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code