四次挥手
四次挥手
四次挥手是客户端和服务端进行关闭连接前置动作。
实战
四次挥手基本流程图,也就是下图中的 segment4-7
提示
FIN ACK
请查看三次握手
使用代码进行 DNS 查询,并使用WireShark
进行抓包,抓包使用 filter 进行过滤,过滤条件为tcp.port == 53
,过滤后的结果如下图所示:
TCP 是双工的,所以涉及到半关问题,一般情况下需要 4 次挥手,在合适的场景使用 3 次挥手(合并一次挥手)
提示
There are six flag bits in the TCP header. One or more of them can be turned on at the same time. We briefly mention their use here and discuss each flag in more detail in later chapters.
在这里有一句话, One or more of them can be turned on at the same time.
在一个段中可能会出现多个 flag
- URG The urgent pointer is valid (Section 20.8).
- ACK The acknowledgment number is valid.
- PSH The receiver should pass this data to the application as soon as possible (Section 20.5).
- RST Reset the connection (Section 18.7).
- SYN Synchronize sequence numbers to initiate a connection. This flag and the next are described in Chapter 18.
- FIN The sender is finished sending data.
看一下三次挥手,client 发送完数据,发送一个 FIN 数据段,其中 client 序列号为 36
server 数据发送完了,所以是可以断开的,所以这里将FIN ACK
进行合并了,对 client 的序号进行+1 确认,并给出 server 序号 52,这个时候 server 对 client 的写连接已经关闭,并发送读断开的 FIN
client 收到后,对 server 序号+1,发送到 server,让 server 进行确认
半关
查看半关案例
图来自《TCP IP 协议详解卷一:协议》
如果想产生 4 次挥手,最好的测试就是 client 发送数据,发送写数据请求,然后 server 发送数据,使用代码进行半关测试
wireshark 抓本地环绕,使用tcp.port == 4444
过滤条件进行查看
- no.329 client 发送 FIN
- no.330 server ACK
- no.415 server 发送数据
- no.416 数据的 data 的 ACK
- no.417 server 发出 FIN
- no.418 client 发出 ACK