MQTT - Connect报文解析

Posted wuwangchuxin0924

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MQTT - Connect报文解析相关的知识,希望对你有一定的参考价值。

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s[82] = {
    /*
     * 固定报头: MQTT报文类型(1), 保留位
     */
      "0001 0000", 
    // 剩余长度(80)
      "01010000",

    /*
     * 可变报头
     */
    // MSB
      "00000000", 
    // LSB
      "00000110", 
    // 协议名: M Q I s d p
      "01001101", "01010001", "01001001", "01110011", "01100100", "01110000", 
    // 协议级别(3)
      "00000011", 
    // 连接标志(usernameflag(true), passwordflag(true), willretain(true), willqos(1), willflag(true), cleansession(true), reserved(0))
      "11101110", 
    // keeplive: MSB LSB (120s)
      "00000000", "01111000",
    
    /*
     * 有效载荷
     */
    // Client Id: MSB LSB (21)
      "00000000", "00010101", 
    // 7 y 0 w Q 8 V N J 4 l m B E i 2 R H O S N
      "00110111", "01111001", "00110000", "01110111", "01010001", 
      "00111000", "01010110", "01001110", "01001010", "00110100",
      "01101100", "01101101", "01000010", "01000101", "01101001", 
      "00110010", "01010010", "01001000", "01001111", "01010011", 
      "01001110", 
    // Will Topic: MSB LSB(3)
      "00000000", "00000011", 
    // A a a
      "01000001", "01100001", "01100001", 
    // Will Message: MSB LSB(24)
      "00000000", "00011000", 
    // i   a m   a   l a s t   w i l l   m e s s a g e
      "01101001", "00100000", "01100001", "01101101", "00100000", 
      "01100001", "00100000", "01101100", "01100001", "01110011", 
      "01110100", "00100000", "01110111", "01101001", "01101100", 
      "01101100", "00100000", "01101101", "01100101", "01110011", 
      "01110011", "01100001", "01100111", "01100101",
    // User name: MSB LSB(3)
      "00000000", "00000100", 
    // A o m i
      "01000001", "01101111", "01101101", "01101001", 
    // Password: MSB LSB(6)
      "00000000", "00000110", 
    // 1 2 3 4 5 6
      "00110001", "00110010", "00110011", "00110100", "00110101", 
      "00110110"};
  for (int i = 0; i < 82; i++) {
    int ret = 0;
    for (int j = 0; j < s[i].size(); j++) {
      ret *= 2;
      if (s[i][j] == 1) ret++;
    }
    if (ret)
      printf("%c|", (ret));
    else
      printf(" |");
    if (i % 6 == 5) {
      puts("");
    }
  }
  return 0;
}

 

以上是关于MQTT - Connect报文解析的主要内容,如果未能解决你的问题,请参考以下文章

MQTT——控制报文格式

物联网之MQTT3.1.1和MQTT5协议 (21) MQTT系列博文总览

MQTT之固定报头分析

MQTT之固定报头分析

MQTT协议探究

Linux应用开发第十六章MQTT协议分析应用开发