我是靠谱客的博主 体贴星星,最近开发中收集的这篇文章主要介绍paho mqtt调用MQTTClient_connect失败,返回MQTTCLIENT_BAD_STRUCTURE(-8),觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
1、问题描述
使用编译好的库文件,从githup上下载头文件,运行时MQTTClient_connect调用失败,返回MQTTCLIENT_BAD_STRUCTURE;
MQTTCLIENT_BAD_STRUCTURE在MQTTClient.h中是struct_id或struct_version 不正确,其定义为:
/**
* Return code: A structure parameter does not have the correct eyecatcher
* and version number.
*/
#define MQTTCLIENT_BAD_STRUCTURE -8
2、问题原因
该问题是由于mqtt库版本为1.1,而头文件版本为1.3,1.3版本的mqtt struct_version 值为6,在1.1版本不兼容,以下是mqtt 1.1和1.3版本MQTTClient_connect函数部分代码:
//paho mqtt 1.1版本
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options)
{
MQTTClients* m = handle;
int rc = SOCKET_ERROR;
FUNC_ENTRY;
Thread_lock_mutex(connect_mutex);
Thread_lock_mutex(mqttclient_mutex);
if (options == NULL)
{
rc = MQTTCLIENT_NULL_PARAMETER;
goto exit;
}
if (strncmp(options->struct_id, "MQTC", 4) != 0 ||
(options->struct_version != 0 && options->struct_version != 1 && options->struct_version != 2
&& options->struct_version != 3 && options->struct_version != 4))
{
rc = MQTTCLIENT_BAD_STRUCTURE;
goto exit;
}
.......
}
//paho mqtt 1.3版本
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options)
{
MQTTClients* m = handle;
if (m->c->MQTTVersion >= MQTTVERSION_5)
return MQTTCLIENT_WRONG_MQTT_VERSION;
MQTTResponse response = MQTTClient_connectAll(handle, options, NULL, NULL);
return response.reasonCode;
}
MQTTResponse MQTTClient_connectAll(MQTTClient handle, MQTTClient_connectOptions* options,
MQTTProperties* connectProperties, MQTTProperties* willProperties)
{
MQTTClients* m = handle;
MQTTResponse rc = MQTTResponse_initializer;
FUNC_ENTRY;
Thread_lock_mutex(connect_mutex);
Thread_lock_mutex(mqttclient_mutex);
rc.reasonCode = SOCKET_ERROR;
if (options == NULL)
{
rc.reasonCode = MQTTCLIENT_NULL_PARAMETER;
goto exit;
}
if (strncmp(options->struct_id, "MQTC", 4) != 0 || options->struct_version < 0 || options->struct_version > 6)
{
rc.reasonCode = MQTTCLIENT_BAD_STRUCTURE;
goto exit;
}
.....
}
最后
以上就是体贴星星为你收集整理的paho mqtt调用MQTTClient_connect失败,返回MQTTCLIENT_BAD_STRUCTURE(-8)的全部内容,希望文章能够帮你解决paho mqtt调用MQTTClient_connect失败,返回MQTTCLIENT_BAD_STRUCTURE(-8)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复