TCP三次握手四次挥手
🧑💻 User
The TCP Three way Handshake
Step 1: The TCP client sends a special segment (containing no data) with the SYN bit set to 1. The client also generates an initial sequence number (client_isn) and includes it in this special TCP SYN segment.
Step 2: The server, upon receiving this packet, allocates the required resources for the connection and sends back the special “connection-granted” segment which we call SYNACK segment. This packet has the SYN bit set to 1, the acknowledgement field of the TCP segment header set to client_isn+1, and a randomly chosen initial sequence number (server_isn) for the server.
Step 3: When the client receives the SYNACK segment, it also allocates buffer and resources for the connection and sends an acknowledgment with SYN bit set to 0.
Connection Teardown
Step 1: When the client wants to end the connection, it sends a segment with FIN bit set to 1 to the server.
Step 2: The server acknowledges that it has received the connection closing request and is now working on closing the connection.
Step 3: The server then sends a segment with FIN bit set to 1, indicating that connection is closed.
Step 4: The client sends an ACK for it to the server. It also waits for sometime to resend this acknowledgment in case the first ACK segment is lost.
The TCP Three-Way Handshake-2
Errata:
Video at 1:01 - The video states the ACK has the SYN bit set to 1. The SYN bit is set to 0 in the ACK from the client. The text is correct.
Reference:
Kurose-Ross Edition 6, Section 3.5.6
🤖 Assistant
TCP 三次握手与连接断开 - 费曼学习法自检清单
📋 自检 Checklist
第一部分:TCP 三次握手 (Three-way Handshake)
| 序号 | 自检问题 | 能否清晰回答? |
|---|---|---|
| 1 | 三次握手的目的是什么?为什么需要”三次”而不是两次? | ☐ |
| 2 | 第一次握手:客户端发送的 SYN 段有什么特点?包含哪些关键信息? | ☐ |
| 3 | 什么是 client_isn?它的作用是什么? |
☐ |
| 4 | 第二次握手:服务器返回的 SYNACK 段包含哪些字段?确认号 (ACK number) 设置为多少? | ☐ |
| 5 | 服务器在什么时候分配连接资源 (allocate resources)? | ☐ |
| 6 | 第三次握手:客户端发送的确认段中,SYN 位设置为多少? | ☐ |
| 7 | 你能画出三次握手的时序图吗? | ☐ |
第二部分:连接断开 (Connection Teardown)
| 序号 | 自检问题 | 能否清晰回答? |
|---|---|---|
| 8 | 连接断开需要几个步骤?为什么比建立连接多一步? | ☐ |
| 9 | FIN 位 (FIN bit) 的作用是什么? | ☐ |
| 10 | 为什么服务器需要发送两次响应(一次 ACK,一次 FIN)? | ☐ |
| 11 | 客户端在最后为什么要”等待一段时间” (wait for sometime)? | ☐ |
| 12 | 你能用自己的话向一个小白解释整个断开过程吗? | ☐ |
第三部分:综合理解
| 序号 | 自检问题 | 能否清晰回答? |
|---|---|---|
| 13 | ISN (Initial Sequence Number) 为什么要随机生成? | ☐ |
| 14 | 三次握手和四次挥手中,哪些段携带数据,哪些不携带? | ☐ |
| 15 | 如果让你类比成现实生活中的场景(如打电话),你会怎么解释? | ☐ |
✅ 参考答案
第一部分答案:TCP 三次握手
1. 三次握手的目的
- 建立可靠的双向通信连接
- 双方确认彼此的发送和接收能力
- 同步双方的初始序列号 (ISN)
2-3. 第一次握手 (Step 1)
- 客户端 → 服务器
- SYN 位 = 1
- 不携带数据 (containing no data)
- 包含客户端随机生成的初始序列号
client_isn
4-5. 第二次握手 (Step 2)
- 服务器 → 客户端
- SYN 位 = 1
- 确认号 (Acknowledgement) =
client_isn + 1 - 包含服务器随机生成的
server_isn - 此时服务器分配资源
6. 第三次握手 (Step 3)
- 客户端 → 服务器
- SYN 位 = 0(注意:不是1!)
- 客户端此时分配缓冲区和资源
第二部分答案:连接断开(四次挥手)
8. 四个步骤的原因
- TCP 是全双工 (full-duplex),双方都需要独立关闭各自的发送通道
9. FIN 位
- FIN = Finish,表示发送方已完成数据发送,请求关闭连接
10. 服务器两次响应的原因
- 第一次 ACK:确认收到关闭请求
- 第二次 FIN:服务器处理完剩余数据后,正式关闭
11. 客户端等待的原因
- 防止最后的 ACK 丢失
- 如果丢失,服务器会重发 FIN,客户端需要能够重发 ACK
第三部分答案:综合理解
13. ISN 随机生成的原因
- 安全性:防止 TCP 序列号预测攻击
- 避免与旧连接的数据包混淆
14. 数据携带情况
- 三次握手的前两个段:不携带数据
- 第三个 ACK:可以携带数据
- 四次挥手:通常不携带数据
15. 生活类比(打电话)
1 | 三次握手: |
🔑 关键术语对照表
| 中文 | English |
|---|---|
| 三次握手 | Three-way Handshake |
| 初始序列号 | Initial Sequence Number (ISN) |
| 确认号 | Acknowledgement Number |
| 连接断开 | Connection Teardown |
| 分配资源 | Allocate Resources |
| 全双工 | Full-duplex |
