[8] mqtt,mosquitto,eclipse Paho---MQTT message format subscribe (message subscription) message analysis

Source: Internet
Author: User

0. Preface

In the last few sections of the protocol analysis, the request to send the connection was using the Eclipse Paho mqtt tool, so this time I changed the code to send the Subscribe MQTT message, please note that the following code is based on the Eclipse Paho Java API code, before running the following code, please download the Eclipse Paho Java Library, which is: https://www.eclipse.org/paho/clients/java/. Let's get down to the chase.

1. Preparation Steps

(1) First open the Wireshark software and start monitoring

(2) Run the following code in eclipse

Import Org.eclipse.paho.client.mqttv3.mqttclient;import Org.eclipse.paho.client.mqttv3.mqttconnectoptions;import Org.eclipse.paho.client.mqttv3.mqttexception;import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; Import Com.tibco.mqtt.test.custommqttcallback;public class Subscriberexample {/** * @param args */public static void main (string[] args)         {string[] topicfilters ={"Topic1", "Topic2"};         String content = "Message from Mqttpublishsample";         int QoS = 0;         String broker = "tcp://192.168.80.196:1883";         String clientId = "Paho-1";          Memorypersistence persistence = new memorypersistence ();                 try {mqttclient sampleclient = new Mqttclient (broker, Clientid,persistence);                 Mqttconnectoptions connopts = new Mqttconnectoptions ();                 Connopts.setcleansession (FALSE); System.                 Out. println ("Connecting to Broker:" + broker);          Sampleclient.connect (connopts);       System.                 Out. println ("Connected");                 Sampleclient.setcallback (New Custommqttcallback ());                 Sampleclient.subscribe (Topicfilters,new int[]{1,1}); System.         Out. println ("Subscribe Success for:" +topicfilters.tostring ());                 } catch (Mqttexception me) {System. Out. println ("Reason" + me.getreasoncode ()); System.                 Out. println ("msg" + me.getmessage ()); System.                 Out. println ("loc" + Me.getlocalizedmessage ()); System.                 Out. println ("Cause" + me.getcause ()); System.                 Out. println ("excep" + Me);         Me.printstacktrace (); }}}


(3) After running, the Wireshark will be able to crawl to the following TCP packets.



2. Protocol Analysis

The following is the specific protocol analysis, the address of the Protocol (http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718063)


2.1 Fixing Head (fixed header)The message format for SUBSCRIBE fixed headers is shown in the following table

Table 1–subscribe Fixed Header message format

Bit

7

6

5

4

3

2

1

0

BYTE 1

MQTT Control Packet Type (8)

Reserved

1

0

0

0

0

0

1

0

BYTE 2

Remaining Length



The data obtained from the packet Capture program shows that the fixed head of the 16 binary is: 82 14

In conjunction with table 1 above, we know that the specific meanings are as follows:

---1000 0010 indicates that the connection request is subscribe

14--1*16+4=20 indicates that the following 20 bytes will be followed


2.2 Variable Head (Variable header)

The message format for SUBSCRIBE variable headers is shown in the following table

Table 2-Message format for variable headers

Description

7

6

5

4

3

2

1

0

Packet Identifier

BYTE 1

Packet Identifier MSB (0)

0

0

0

0

0

0

0

0

BYTE 2

Packet Identifier LSB (10)

0

0

0

0

1

0

1

0

The data obtained from the gripper program shows that the variable head 16 binary is: 00 01

In conjunction with table 1 above, we know that the specific meanings are as follows:

---the current package ID is 1


2.3 Load Section (payload)

The message format for the SUBSCRIBE Payload section is shown in the following table, the payload section primarily specifies the name of the subscription theme (Topic) and the QoS level for each subscribed topic (total three types of values: 0,1,2), and it is important to note that in the Load section, multiple topics can be subscribed to at one time.

Table 3–subscribe message format for load section

Description

7

6

5

4

3

2

1

0

Topic Filter

BYTE 1

Length MSB

BYTE 2

Length LSB

Bytes 3..N

Topic Filter

Requested QoS

Reserved

Qos

BYTE n+1

0

0

0

0

0

0

X

X


The data obtained from the gripper program indicates that the load part The data is actually captured as follows: 6f, 6f, and more. Because there are a total of two topics: Topic1 and Topic2, so we take the first topic (TOPIC1) to analyze the line, the following topic similar.

00 06 represents the length of this topic is 6 bytes
6f 70 69 63 31 represents TOPIC1

01 for QoS of 1


Congratulations, you already know how to analyze the message format of MQTT subscribe, it is not very magical, and in fact, the analysis protocol is not difficult, so simple. In the next section, I will continue to analyze the SUBACK of Mqtt (the answer to the subscription request message). Please look forward to and attention.



[8] mqtt,mosquitto,eclipse Paho---MQTT message format subscribe (message subscription) message analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.