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;
}

猜你喜欢

转载自www.cnblogs.com/wuwangchuxin0924/p/10639700.html
今日推荐