当前位置:网站首页>你需要知道的 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
我是前端西瓜哥,欢迎关注我,掌握更多前端面试题。
边栏推荐
- SQL injection vulnerability (MSSQL injection)
- Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
- 【Try to Hack】john哈希破解工具
- I'd like to ask about the current MySQL CDC design. In the full volume phase, if a chunk's binlog backfill phase,
- ETCD数据库源码分析——etcdserver bootstrap初始化存储
- Quatre méthodes de redis pour dépanner les grandes clés sont nécessaires pour optimiser
- Dry goods collection | Vulkan game engine video tutorial
- npm命令--安装依赖包--用法/详解
- 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
- Platformio create libopencm3 + FreeRTOS project
猜你喜欢
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
Digital children < daily question> (Digital DP)
Is the mode of education together - on campus + off campus reliable
Visio draws Tai Chi
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
ORM aggregate query and native database operation
The most detailed and comprehensive update content and all functions of guitar pro 8.0
Easyrecovery reliable and toll free data recovery computer software
[05-1, 05-02, 05-03] network protocol
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
随机推荐
P2022 interesting numbers (binary & digit DP)
Basic explanation of turtle module - draw curve
Selection sort
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
The kernel determines whether peripherals are attached to the I2C address
Knowledge consolidation source code implementation 3: buffer ringbuffer
Vulnerability discovery - vulnerability probe type utilization and repair of web applications
Ue5 small knowledge freezerendering view rendered objects in the cone
Redis 排查大 key 的4種方法,優化必備
ETCD数据库源码分析——etcdserver bootstrap初始化存储
Basic knowledge and examples of binary tree
flink sql 能同时读多个topic吗。with里怎么写
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
Dry goods collection | Vulkan game engine video tutorial
Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码
Mysql database storage engine
[try to hack] John hash cracking tool
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
729. My schedule I (set or dynamic open point segment tree)
The value of two date types is subtracted and converted to seconds