调用MQTTClient_connect返回-8

本文针对使用paho.mqtt.c时MQTTClient_connect返回-8的问题进行了详细分析,指出该问题是由于libpaho-mqtt3c.so与头文件版本不匹配导致,并提供了解决方案,即下载相同版本的paho.mqtt.c源码并重新编译。

1、问题描述

使用开源库paho.mqtt.c时,其接口MQTTClient_connect返回-8;查看源文件对应的返回值具体为:MQTTCLIENT_BAD_STRUCTURE(-8)

2、根据分析结论如下:

使用的libpaho-mqtt3c.so和头文件不匹配,头文件是下载的1.3.9版本的paho.mqtt.c,而库是使用其他编译的;

3、解决办法

下载自己需要版本的paho.mqtt.c源码,重新编译后,使用include+lib

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <MQTTClient.h> #include <pthread.h> #define ADDRESS "tcp://broker.emqx.io:1883" #define CLIENTID "MQTT_Combined_Client" #define PUB_TOPIC "raspberry/publish" #define SUB_TOPIC "raspberry/subscribe" #define QOS 1 #define TIMEOUT 10000L #define PAYLOAD "Hello from combined client!" // 全局客户端实例 MQTTClient client; volatile int exit_flag = 0; // 消息到达回调函数 int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message) { printf("\n[SUB] Message arrived\n"); printf(" Topic: %s\n", topicName); printf(" Message: %.*s\n\n", message->payloadlen, (char*)message->payload); MQTTClient_freeMessage(&message); MQTTClient_free(topicName); return 1; } // 连接丢失回调 void connlost(void *context, char *cause) { printf("\n[CONN] Connection lost! Cause: %s\n", cause); } // 发布线程函数 void* publisher_thread(void* arg) { MQTTClient_message pubmsg = MQTTClient_message_initializer; MQTTClient_deliveryToken token; pubmsg.payload = PAYLOAD; pubmsg.payloadlen = strlen(PAYLOAD); pubmsg.qos = QOS; pubmsg.retained = 0; while (!exit_flag) { int rc = MQTTClient_publishMessage(client, PUB_TOPIC, &pubmsg, &token); if (rc != MQTTCLIENT_SUCCESS) { printf("[PUB] Publish failed: %d\n", rc); } else { MQTTClient_waitForCompletion(client, token, TIMEOUT); printf("[PUB] Message published: %s\n", PAYLOAD); } sleep(5); // 每5秒发布一次 } return NULL; } int main() { MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; pthread_t pub_thread; int rc; // 创建MQTT客户端 rc = MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL); if (rc != MQTTCLIENT_SUCCESS) { fprintf(stderr, "Client creation failed: %d\n", rc); exit(EXIT_FAILURE); } // 设置回调函数 rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, NULL); if (rc != MQTTCLIENT_SUCCESS) { fprintf(stderr, "Callback setup failed: %d\n", rc); MQTTClient_destroy(&client); exit(EXIT_FAILURE); } // 连接配置 conn_opts.keepAliveInterval = 20; conn_opts.cleansession = 1; // 连接到代理 if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) { fprintf(stderr, "Connection failed: %d\n", rc); MQTTClient_destroy(&client); exit(EXIT_FAILURE); } printf("[CONN] Connected to %s\n", ADDRESS); // 订阅主题 rc = MQTTClient_subscribe(client, SUB_TOPIC, QOS); if (rc != MQTTCLIENT_SUCCESS) { fprintf(stderr, "Subscribe failed: %d\n", rc); MQTTClient_disconnect(client, TIMEOUT); MQTTClient_destroy(&client); exit(EXIT_FAILURE); } printf("[SUB] Subscribed to %s\n", SUB_TOPIC); // 创建发布线程 if (pthread_create(&pub_thread, NULL, publisher_thread, NULL) != 0) { perror("Thread creation failed"); MQTTClient_disconnect(client, TIMEOUT); MQTTClient_destroy(&client); exit(EXIT_FAILURE); } // 主线程等待退出指令 printf("\n[SYSTEM] Running (Press Q+Enter to exit)\n"); while (getchar() != &#39;q&#39;) {} exit_flag = 1; // 清理资源 pthread_join(pub_thread, NULL); MQTTClient_unsubscribe(client, SUB_TOPIC); MQTTClient_disconnect(client, TIMEOUT); MQTTClient_destroy(&client); printf("\n[SYSTEM] Client shutdown\n"); return EXIT_SUCCESS; }将库<MQTTClient.h>改为使用<mosquitto.h>
09-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值