In the previous section ([6] mqtt,mosquitto,eclipse Paho---The MQTT message format for connect message format analysis) We analyzed the Connect message format, and we knew that the connect message was sent out by the client, As a connection request to the client, the server side will also have a return of the message, which is the connack message. After we send the Connect message, if the Wireshark capture tool is still turned on, it will catch a TCP message similar to the following, its 16 binary is: 20 02 00 00, then what does it mean?
First, let's look at the message format of Connack. The connack message does not have a message payload (Payload), only the message header: 2 bytes of fixed message header and 22 bytes of variable message headers. A total of 4 bytes
1. Fix header (fixed header): 16 binary data: 20 02
For the specific protocol format, see table 1 below
Table 1 – Format of fixed header
Bit |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
BYTE 1 |
MQTT Control Packet Type (2) |
Reserved |
|
0 |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
BYTE 2 |
Remaining Length (2) |
|
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0
|
20 indicates that its current message type is: CONNACK
02 indicates that the following 2 bytes are followed.
2. Variable message header (Variable header): 16 binary data is: XX
For the specific protocol format, see table 2 below
Table 2 – Format of variable message header (Variable header)
|
Describe |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
Connection answer flag bit |
Reserved (not used) |
SP1 |
BYTE 1 |
|
0 |
0 |
0 |
0 |
0 |
0 |
0 |
X |
Connection results returned |
BYTE 2 |
|
X |
X |
X |
X |
X |
X |
X |
X
|
Let's analyze the 2 bytes of its variable header: 00 00
The first 00, which currently has no special meaning, is a reserved field for the MQTT protocol and may be used in future protocol versions.
A 00 of the second byte indicates a successful connection. The MQTT protocol defines a total of 6 types for the returned results (see table 3 below): Connection accepted, connection denied (unacceptable version), connection denied (Client ID server not allowed), connection denied ( server unreachable), connection denied (bad user name and password), connection denied (client not authenticated by authorization).
Table 3-Meaning of the return code value of the connection answer
Value |
The connection code returned |
Describe |
0 |
0x00 Connection Accepted |
Connection accepted |
1 |
0x01 Connection denied (unacceptable version) |
The service side does not support the protocol of the connected Mqtt |
2 |
0x02 connection denied (Client ID server not allowed) |
The client ID complies with the UTF-8 standard, but the server does not allow this client ID |
3 |
0x03 Connection denied (server unreachable) |
MQTT Server Unreachable |
4 |
0x04 connection denied (bad user name and password) |
Bad user name and password
|
5 |
0x05 Connection denied (client not authenticated by authorization) |
Client does not have authorization authentication |
6-255 |
|
Future reserved Fields
|
This message format is simple, hey, congratulations on your journey to learn the MQTT message format and move forward a step further, the next chapter, I will continue to explainSubscribe message Format, please pay attention and look forward to, thank you.
[7] mqtt,mosquitto,eclipse Paho---MQTT message format connack message analysis