0. 准备
- wireshark安装包:网络抓包工具
- qmqtt源码:mqtt客户端源码(Qt版本)
- 官网:http://mqtt.org/
- 英文版协议说明文档:http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
- 中文版协议说明文档 https://mcxiaoke.gitbooks.io/mqtt-cn/content/mqtt/01-Introduction.html
1、帧
1.1帧结构
Fixed header, present in all MQTT Control Packets |
Variable header, present in some MQTT Control Packets |
Payload, present in some MQTT Control Packets |
帧 = 固定头+可变头+载荷
1.2 帧类型
name | value | drirection | Description |
---|---|---|---|
Reserved | 0 | Forbidden | Reserved |
CONNECT | 0 | Client to Server | Client request to connect to Server |
CONNACK | 0 | Server to Client | Connect acknowledgment |
PUBLISH | 0 | Client to Server or Server to Client | Publish message |
PUBACK | 0 | 同上 | Publish acknowledgment |
PUBREC | 0 | 同上 | Publish received (assured delivery part 1) |
PUBREL | 0 | 同上 | Publish release (assured delivery part 2) |
PUBCOMP | 0 | 同上 | Publish complete (assured delivery part 3) |
SUBSCRIBE | 0 | Client to Server | Client subscribe request |
SUBACK | 0 | Server to Client | Subscribe acknowledgment |
UNSUBSCRIBE | 0 | Client to Server | Unsubscribe request |
UNSUBACK | 0 | Server to Client | Unsubscribe acknowledgment |
PINGREQ | 0 | Client to Server | PING request |
PINGRESP | 0 | Server to Client | PING response |
DISCONNECT | 0 | Client to Server | Client is disconnecting |
Reserved | 0 | Forbidden | Reserved |
2、 wireshark 使用
打开wireshark 后主界面会显示网络的连接有那些,如果安装了虚拟机的话会有如下的连接:
选择 realtek PCIe……..
双击进入如下界面
选择过滤为 mqtt(见左上角),这样的话wireshark就只会显示MQTT协议的包了。
3、connect 帧
与服务器连接时可以使用will topic 和will msg,
它俩的作用是什么呢?
就是在这个客户端非正常的退出时,服务器会推送这个topic给定阅了这个topic的客户端。
这里我们的client id没有填写,而在我们的抓包中(下图)可以看到到一个字串,这是QMQTT生成的一个uuid
对照协议文档再看wrieshark所抓的包,就可以清楚的明白,帧结构了。
4、subscribe 帧
这里我们定阅了一个主题是:guangzhou/huangpu
抓出来的包如下:
5、publish 帧
这里我们发布了一个主题消息:work here
以下是抓到的包:
6、总结
其它的帧可以通过同样的方法可以抓到,如此我们就可以根据数据来分析帧的结构了。(对照文档)
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
</div>