日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

paho mqtt client调试记录

發(fā)布時(shí)間:2025/3/21 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 paho mqtt client调试记录 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

官網(wǎng):http://www.eclipse.org/paho/clients/c/

編譯流程:

git clone https://github.com/eclipse/paho.mqtt.c.git cd org.eclipse.paho.mqtt.c.git make sudo make install

?

編譯過(guò)程中報(bào)找不到clock_gettime函數(shù)的錯(cuò)誤,在Makefile中相關(guān)位置添加-lrt就可以了。

make后sample里的測(cè)試代碼也會(huì)跟著成功編譯。

也可以在sample目錄下單獨(dú)用命令編譯:gcc -I../ -L../../../paho.mqtt.c/build/output/ MQTTClient_subscribe.c -lpaho-mqtt3c -o MQTTTest -lrt

?

我測(cè)試了訂閱部分,修改了原版demo,同時(shí)和服務(wù)器建立4個(gè)連接,每個(gè)連接又訂閱了4個(gè)主題。通過(guò)對(duì)主題進(jìn)行數(shù)據(jù)發(fā)送和抓包分析,都顯示沒(méi)問(wèn)題。

修改后的代碼如下:

/*******************************************************************************
?* Copyright (c) 2012, 2017 IBM Corp.
?*
?* All rights reserved. This program and the accompanying materials
?* are made available under the terms of the Eclipse Public License v1.0
?* and Eclipse Distribution License v1.0 which accompany this distribution.?
?*
?* The Eclipse Public License is available at?
?* ? http://www.eclipse.org/legal/epl-v10.html
?* and the Eclipse Distribution License is available at?
?* ? http://www.eclipse.org/org/documents/edl-v10.php.
?*
?* Contributors:
?* ? ?Ian Craggs - initial contribution
?*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"

//#define ADDRESS ? ? "tcp://localhost:1883"
//#define ADDRESS ? ? "192.168.0.167"
#define ADDRESS ? ? "192.168.0.117"
#define CLIENTID ? ?"ExampleClientSub"
#define TOPIC ? ? ? "1234/qinrenzhi"
#define TOPIC_11 ? ?"1234/qinrenzhi_11"
#define TOPIC_12 ? ?"1234/qinrenzhi_12"
#define TOPIC_13 ? ?"1234/qinrenzhi_13"
#define TOPIC_14 ? ?"1234/qinrenzhi_14"

#define PAYLOAD ? ? "Hello World!"
#define QOS ? ? ? ? 1
#define TIMEOUT ? ? 10000L

#define CLIENTID_2 ? ?"ExampleClientSub_2"
#define TOPIC_2 ? ? ? "1234/qinrenzhi_2"
#define TOPIC_21 ? ? ?"1234/qinrenzhi_21"
#define TOPIC_22 ? ? ?"1234/qinrenzhi_22"
#define TOPIC_23 ? ? ?"1234/qinrenzhi_23"

#define CLIENTID_3 ? ?"ExampleClientSub_3"
#define TOPIC_3 ? ? ? "1234/qinrenzhi_3"
#define TOPIC_31 ? ? ? "1234/qinrenzhi_31"
#define TOPIC_32 ? ? ? "1234/qinrenzhi_32"
#define TOPIC_33 ? ? ? "1234/qinrenzhi_33"

#define CLIENTID_4 ? ?"ExampleClientSub_4"
#define TOPIC_4 ? ? ? "1234/qinrenzhi_4"
#define TOPIC_41 ? ? ?"1234/qinrenzhi_41"
#define TOPIC_42 ? ? ?"1234/qinrenzhi_42"
#define TOPIC_43 ? ? ?"1234/qinrenzhi_43"


volatile MQTTClient_deliveryToken deliveredtoken_2;

void delivered_2(void *context, MQTTClient_deliveryToken dt)
{
? ? printf("2:Message with token value %d delivery confirmed\n", dt);
? ? deliveredtoken_2 = dt;
}

int msgarrvd_2(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
? ? int i;
? ? char* payloadptr;

? ? printf("2:Message arrived\n");
? ? printf("2: ? ? topic: %s\n", topicName);
? ? printf("2: ? message: ");

? ? payloadptr = message->payload;
? ? for(i=0; i<message->payloadlen; i++)
? ? {
? ? ? ? putchar(*payloadptr++);
? ? }
? ? putchar('\n');
? ? MQTTClient_freeMessage(&message);
? ? MQTTClient_free(topicName);
? ? return 1;
}

void connlost_2(void *context, char *cause)
{
? ? printf("\n2:Connection lost\n");
? ? printf(" ? ? cause: %s\n", cause);
}

volatile MQTTClient_deliveryToken deliveredtoken_3;

void delivered_3(void *context, MQTTClient_deliveryToken dt)
{
? ? printf("3:Message with token value %d delivery confirmed\n", dt);
? ? deliveredtoken_3 = dt;
}

int msgarrvd_3(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
? ? int i;
? ? char* payloadptr;

? ? printf("3:Message arrived\n");
? ? printf("3: ? ? topic: %s\n", topicName);
? ? printf("3: ? message: ");

? ? payloadptr = message->payload;
? ? for(i=0; i<message->payloadlen; i++)
? ? {
? ? ? ? putchar(*payloadptr++);
? ? }
? ? putchar('\n');
? ? MQTTClient_freeMessage(&message);
? ? MQTTClient_free(topicName);
? ? return 1;
}

void connlost_3(void *context, char *cause)
{
? ? printf("\n3:Connection lost\n");
? ? printf(" ? ? cause: %s\n", cause);
}


volatile MQTTClient_deliveryToken deliveredtoken_4;

void delivered_4(void *context, MQTTClient_deliveryToken dt)
{
? ? printf("4:Message with token value %d delivery confirmed\n", dt);
? ? deliveredtoken_4 = dt;
}

int msgarrvd_4(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
? ? int i;
? ? char* payloadptr;

? ? printf("4:Message arrived\n");
? ? printf("4: ? ? topic: %s\n", topicName);
? ? printf("4: ? message: ");

? ? payloadptr = message->payload;
? ? for(i=0; i<message->payloadlen; i++)
? ? {
? ? ? ? putchar(*payloadptr++);
? ? }
? ? putchar('\n');
? ? MQTTClient_freeMessage(&message);
? ? MQTTClient_free(topicName);
? ? return 1;
}

void connlost_4(void *context, char *cause)
{
? ? printf("\n4:Connection lost\n");
? ? printf(" ? ? cause: %s\n", cause);
}


volatile MQTTClient_deliveryToken deliveredtoken;

void delivered(void *context, MQTTClient_deliveryToken dt)
{
? ? printf("Message with token value %d delivery confirmed\n", dt);
? ? deliveredtoken = dt;
}

int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
? ? int i;
? ? char* payloadptr;

? ? printf("Message arrived\n");
? ? printf(" ? ? topic: %s\n", topicName);
? ? printf(" ? message: ");

? ? payloadptr = message->payload;
? ? for(i=0; i<message->payloadlen; i++)
? ? {
? ? ? ? putchar(*payloadptr++);
? ? }
? ? putchar('\n');
? ? MQTTClient_freeMessage(&message);
? ? MQTTClient_free(topicName);
? ? return 1;
}

void connlost(void *context, char *cause)
{
? ? printf("\nConnection lost\n");
? ? printf(" ? ? cause: %s\n", cause);
}

int main(int argc, char* argv[])
{
? ? MQTTClient client;
? ? MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
? ? int rc;
? ? int ch;

? ? MQTTClient_create(&client, ADDRESS, CLIENTID,
? ? ? ? MQTTCLIENT_PERSISTENCE_NONE, NULL);
? ? conn_opts.keepAliveInterval = 200;
? ? conn_opts.cleansession = 1;

? ? MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);

? ? if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
? ? {
? ? ? ? printf("Failed to connect, return code %d\n", rc);
? ? ? ? exit(EXIT_FAILURE);
? ? }
? ? printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
? ? ? ? ? ?"Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
? ? MQTTClient_subscribe(client, TOPIC, QOS);
? ? MQTTClient_subscribe(client, TOPIC_11, QOS);
? ? MQTTClient_subscribe(client, TOPIC_12, QOS);
? ? MQTTClient_subscribe(client, TOPIC_13, QOS);

? ? MQTTClient client_2;
? ? MQTTClient_connectOptions conn_opts_2 = MQTTClient_connectOptions_initializer;
? ? int rc_2;
? ? int ch_2;

? ? MQTTClient_create(&client_2, ADDRESS, CLIENTID_2,
? ? ? ? MQTTCLIENT_PERSISTENCE_NONE, NULL);
? ? conn_opts_2.keepAliveInterval = 200;
? ? conn_opts_2.cleansession = 1;

? ? MQTTClient_setCallbacks(client_2, NULL, connlost_2, msgarrvd_2, delivered_2);

? ? if ((rc_2 = MQTTClient_connect(client_2, &conn_opts_2)) != MQTTCLIENT_SUCCESS)
? ? {
? ? ? ? printf("2:Failed to connect, return code %d\n", rc);
? ? ? ? exit(EXIT_FAILURE);
? ? }
? ? printf("2:Subscribing to topic %s\nfor client %s using QoS%d\n\n"
? ? ? ? ? ?"Press Q<Enter> to quit\n\n", TOPIC_2, CLIENTID_2, QOS);
? ? MQTTClient_subscribe(client_2, TOPIC_2, QOS);
? ? MQTTClient_subscribe(client_2, TOPIC_21, QOS);
? ? MQTTClient_subscribe(client_2, TOPIC_22, QOS);
? ? MQTTClient_subscribe(client_2, TOPIC_23, QOS);


? ? MQTTClient client_3;
? ? MQTTClient_connectOptions conn_opts_3 = MQTTClient_connectOptions_initializer;
? ? int rc_3;
? ? int ch_3;

? ? MQTTClient_create(&client_3, ADDRESS, CLIENTID_3,
? ? ? ? MQTTCLIENT_PERSISTENCE_NONE, NULL);
? ? conn_opts_3.keepAliveInterval = 200;
? ? conn_opts_3.cleansession = 1;

? ? MQTTClient_setCallbacks(client_3, NULL, connlost_3, msgarrvd_3, delivered_3);

? ? if ((rc_3 = MQTTClient_connect(client_3, &conn_opts_3)) != MQTTCLIENT_SUCCESS)
? ? {
? ? ? ? printf("3:Failed to connect, return code %d\n", rc);
? ? ? ? exit(EXIT_FAILURE);
? ? }
? ? printf("3:Subscribing to topic %s\nfor client %s using QoS%d\n\n"
? ? ? ? ? ?"Press Q<Enter> to quit\n\n", TOPIC_3, CLIENTID_3, QOS);
? ? MQTTClient_subscribe(client_3, TOPIC_3, QOS);
? ? MQTTClient_subscribe(client_3, TOPIC_31, QOS);
? ? MQTTClient_subscribe(client_3, TOPIC_32, QOS);
? ? MQTTClient_subscribe(client_3, TOPIC_33, QOS);


? ? MQTTClient client_4;
? ? MQTTClient_connectOptions conn_opts_4 = MQTTClient_connectOptions_initializer;
? ? int rc_4;
? ? int ch_4;

? ? MQTTClient_create(&client_4, ADDRESS, CLIENTID_4,
? ? ? ? MQTTCLIENT_PERSISTENCE_NONE, NULL);
? ? conn_opts_4.keepAliveInterval = 200;
? ? conn_opts_4.cleansession = 1;

? ? MQTTClient_setCallbacks(client_4, NULL, connlost_4, msgarrvd_4, delivered_4);

? ? if ((rc_4 = MQTTClient_connect(client_4, &conn_opts_4)) != MQTTCLIENT_SUCCESS)
? ? {
? ? ? ? printf("4:Failed to connect, return code %d\n", rc);
? ? ? ? exit(EXIT_FAILURE);
? ? }
? ? printf("4:Subscribing to topic %s\nfor client %s using QoS%d\n\n"
? ? ? ? ? ?"Press Q<Enter> to quit\n\n", TOPIC_4, CLIENTID_4, QOS);
? ? MQTTClient_subscribe(client_4, TOPIC_4, QOS);
? ? MQTTClient_subscribe(client_4, TOPIC_41, QOS);
? ? MQTTClient_subscribe(client_4, TOPIC_42, QOS);
? ? MQTTClient_subscribe(client_4, TOPIC_43, QOS);

? ? do?
? ? {
? ? ? ? ch = getchar();
? ? } while(ch!='Q' && ch != 'q');

? ? MQTTClient_unsubscribe(client, TOPIC);
? ? MQTTClient_unsubscribe(client, TOPIC_11);
? ? MQTTClient_unsubscribe(client, TOPIC_12);
? ? MQTTClient_unsubscribe(client, TOPIC_13);
? ? MQTTClient_disconnect(client, 10000);
? ? MQTTClient_destroy(&client);


? ? MQTTClient_unsubscribe(client_2, TOPIC);
? ? MQTTClient_unsubscribe(client_2, TOPIC_21);
? ? MQTTClient_unsubscribe(client_2, TOPIC_22);
? ? MQTTClient_unsubscribe(client_2, TOPIC_23);
? ? MQTTClient_disconnect(client_2, 10000);
? ? MQTTClient_destroy(&client_2);

? ? MQTTClient_unsubscribe(client_3, TOPIC);
? ? MQTTClient_unsubscribe(client_3, TOPIC_31);
? ? MQTTClient_unsubscribe(client_3, TOPIC_32);
? ? MQTTClient_unsubscribe(client_3, TOPIC_33); ?
? ? MQTTClient_disconnect(client_3, 10000);
? ? MQTTClient_destroy(&client_3);

? ? MQTTClient_unsubscribe(client_4, TOPIC);
? ? MQTTClient_unsubscribe(client_4, TOPIC_41);
? ? MQTTClient_unsubscribe(client_4, TOPIC_42);
? ? MQTTClient_unsubscribe(client_4, TOPIC_43);
? ? MQTTClient_disconnect(client_4, 10000);
? ? MQTTClient_destroy(&client_4);


? ? return rc;
}
?

總結(jié)

以上是生活随笔為你收集整理的paho mqtt client调试记录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧洲自拍偷拍 | 国产日韩欧美一区二区 | 日本在线不卡一区二区三区 | 亚洲色图国产精品 | 亚洲人无码成www久久 | 激情啪啪网 | 91国内揄拍国内精品对白 | 奇米狠狠 | 啪啪一区二区 | 打白嫩屁屁网站视频短裙 | 伊人情人综合 | 免费日本特黄 | 一级a毛片免费观看久久精品 | 欧美日韩国产精品一区二区三区 | 亚洲一区二区人妻 | 二男一女一级一片 | 一区二区三区免费在线 | 久久久综合网 | 婷婷综合另类小说色区 | wwwxx欧美| 日韩成人短视频 | 亚洲乱亚洲乱 | 亚洲每日更新 | 久久免费视频3 | 久久久久国产精品国产 | 国产精选91 | 色婷婷在线播放 | 好吊妞操 | 亚洲制服一区 | 九色精品 | 人人草人人爽 | av黄色免费网站 | 欧美人与性动交α欧美精品 | 台湾一级视频 | 最新久久久 | 国产一区激情 | 国产精品国产精品 | 久久久久久久久免费看无码 | 亚洲欧美一区二区精品久久久 | 日韩91视频 | 国产91在线看 | 日韩av伦理 | 色婷婷婷婷色 | 日韩欧美高清dvd碟片 | 奇米影视大全 | 日本一二三不卡视频 | www视频在线观看免费 | 看片免费黄在线观看入口 | 动漫大乳美女 | 日韩精品导航 | 国产精品亚洲成在人线 | 国产精品电影院 | 性欧美视频 | 天天插天天干 | 胖女人毛片 | 成人av免费 | 激情欧美一区二区三区 | 日本色www| 久久国产精品国产精品 | аⅴ资源新版在线天堂 | 最近最新中文字幕 | 黄色一区二区三区 | 国产91久久精品一区二区 | 亚洲欧美日韩精品色xxx | 日韩一区二区三区精 | 久久久久久久久久久久97 | 欧美嫩交 | 欧美在线观看不卡 | 亚洲日本三级 | 看黄色一级片 | 国产鲁鲁 | 4438x五月天 日吊视频 | 亚洲天堂小说 | 制服丝袜av一区二区三区下载 | 欧美日韩八区 | 成人免费视频视频 | 小草av在线| 国产又粗又黄又爽的视频 | 色爽视频 | 伊人久久精品视频 | 俄罗斯美女av | 黑人借宿巨大中文字幕 | 我的丝袜美腿尤物麻麻 | 午夜污污| 娇妻玩4p被三个男人伺候电影 | 国产精品无码网站 | 精品国产丝袜一区二区三区乱码 | 欧美精品色视频 | 中文久久字幕 | 国产免费一区,二区 | 中国性xxx | 91免费官网| 成人快色| 日韩欧美不卡 | 日韩一区二区三区精品视频 | 美女视频一区二区三区 | 波多野结衣影院 | 麻豆视频免费网站 | 成人高潮片免费 |