paho mqtt发布连接错误,错误8

时间:2020-06-22 03:21:35

标签: c++ mqtt paho

我的mqtt客户端连接错误,函数“ MQTTAsync_connect”中的错误代码为8

错误:

pi@raspberrypi:~/Downloads/test/lite_cpp $ ./test 
before
Failed to start connect, return code -8

代码:

MQTTAsync client;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc;

MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);

MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);

conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.onSuccess = onConnect;
conn_opts.onFailure = onConnectFailure;
conn_opts.context = client;
printf("before\n");

if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
    printf("Failed to start connect, return code %d\n", rc);
    exit(EXIT_FAILURE);
}

我发现错误代码定义为:https://docs.rs/paho-mqtt-sys/0.2.0/x86_64-apple-darwin/paho_mqtt_sys/fn.MQTTAsync_connect.html

1: Connection refused: Unacceptable protocol version
2: Connection refused: Identifier rejected
3: Connection refused: Server unavailable
4: Connection refused: Bad user name or password
5: Connection refused: Not authorized
6-255: Reserved for future use

但是我知道原因以及如何解决.. 有人有这样的经历吗?

谢谢..

我的publish.cpp如下

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#include <unistd.h>
 
#define ADDRESS     "127.0.0.1:1883"
#define CLIENTID    "ClientPub"
#define TOPIC       "STATUS"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L
 
volatile MQTTAsync_token deliveredtoken;
 
int finished = 0;
bool is_connect = false;

void connlost(void *context, char *cause)
{
    MQTTAsync client = (MQTTAsync)context;
    MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
    int rc;
 
    printf("\nConnection lost\n");
    printf("     cause: %s\n", cause);
 
    printf("Reconnecting\n");
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
    {
        printf("Failed to start connect.., return code %d\n", rc);
        finished = 1;
    }
}
 
 
void onDisconnect(void* context, MQTTAsync_successData* response)
{
    printf("Successful disconnection\n");
    finished = 1;
}
 
 
void onSend(void* context, MQTTAsync_successData* response)
{
    //MQTTAsync client = (MQTTAsync)context;
    //MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
    //int rc;
 
    printf("Message with token value %d delivery confirmed\n", response->token);
      
    //opts.onSuccess = onDisconnect;
    //opts.context = client;
        /* 
    if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
    {
        printf("Failed to start sendMessage, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
          */
}
 
 
void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
    printf("Connect failed, rc %d\n", response ? response->code : 0);
    finished = 1;
}
 
 
void onConnect(void* context, MQTTAsync_successData* response)
{
    MQTTAsync client = (MQTTAsync)context;
    MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
    MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
    int rc;
 
    printf("Successful connection\n");
    is_connect = true;
/*  opts.onSuccess = onSend;
    opts.context = client;
    pubmsg.payload = (void*)PAYLOAD;
    pubmsg.payloadlen = (int)strlen(PAYLOAD);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    deliveredtoken = 0;
        
        if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
        {
        printf("Failed to start sendMessage, return code %d\n", rc);
        exit(EXIT_FAILURE);
        }
*/        
}
 
 
int main(int argc, char* argv[])
{
    MQTTAsync client;
    MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
    int rc;
 
    MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
 
    MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);
 
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;
    conn_opts.onSuccess = onConnect;
    conn_opts.onFailure = onConnectFailure;
    conn_opts.context = client;
    printf("before\n");
    
    if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
    {
        printf("Failed to start connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    
    printf("after\n");
    printf("Waiting for publication of %s\n"
         "on topic %s for client with ClientID: %s\n",
         PAYLOAD, TOPIC, CLIENTID);
    
    while(!is_connect)
    {
       printf("sleep\n");
            usleep(10000L);
    }
        
    MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
       
        opts.onSuccess = onSend;
    opts.context = client;
 
        while(1)
    {
       
       int rc;
           printf("while1\n");
           //#define MQTTAsync_message_initializer 
       MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
       pubmsg.payload = (void*)PAYLOAD;
       pubmsg.payloadlen = (int)strlen(PAYLOAD);
       pubmsg.qos = QOS;
       pubmsg.retained = 0;
       deliveredtoken = 0;
 
       if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
       {
        printf("Failed to start sendMessage, return code %d\n", rc);
        exit(EXIT_FAILURE);
       }
       usleep(100000L);
    }
 
    while (!finished)
        #if defined(WIN32)
            Sleep(100);
        #else
            usleep(10000L);
        #endif
 
    MQTTAsync_destroy(&client);
    return rc;
}

0 个答案:

没有答案