环境: client :ubuntu
broker:windows 32bit
下载mosquitto 的exe版本,直接安装运行,如果不过,请自行上网搜索安装过程以及解决办法;
本文主要讲解paho的demo过程,而且还是参考其他人的博客,博客地址:https://blog.csdn.net/qingdujun/article/details/71055759#mqtt-c-client%E5%AE%9E%E6%88%98
1.首先现在paho的C版本,在github上搜索即可,或者https://github.com/eclipse/paho.mqtt.c,打开源码可以看到里面包含了makefile,所以我没有使用cmake,直接make;可以看到在当前路径下多处了一个build的文件夹,进去可以看到里面有好几个动态库文件。这几个就是我们需要的库文件,(原生的哦,没有裁剪的)。
2.新建demo文件夹(用来实现例子,随便什么地方存放都行),在demo下新建lib、src、include文件夹,将paho源码中的src文件夹中的MQTTClient.h 、 MQTTClientPersistence.h拷贝到include文件夹下,将源码中build文件夹下libpaho-mqtt3c.so.1拷贝到lib下,新建软链接ln -sf libpaho-mqtt3c.so.1.0 libpaho-mqtt3c.so.1 ln -sf libpaho-mqtt3c.so.1 libpaho-mqtt3c.so
同步pub的:
#include "stdio.h" #include "stdlib.h" #include "string.h" #include "MQTTClient.h" #define ADDRESS "tcp://192.168.1.103:1883" #define CLIENTID "ExampleClientPub" #define TOPIC "MQTT Examples" #define PAYLOAD "Hello World!" #define QOS 1 #define TIMEOUT 10000L int main(int argc,char* argv[]) { MQTTClient client; MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; MQTTClient_message pubmsg = MQTTClient_message_initializer; MQTTClient_deliveryToken token; int rc; MQTTClient_create(&client,ADDRESS,CLIENTID,MQTTCLIENT_PERSISTENCE_NONE,NULL); conn_opts.keepAliveInterval = 20; conn_opts.cleansession = 1; if ((rc = MQTTClient_connect(client,&conn_opts)) != MQTTCLIENT_SUCCESS) { printf("Failed to connect,return code %d\n",rc); exit(EXIT_FAILURE); } pubmsg.payload = PAYLOAD; pubmsg.payloadlen = strlen(PAYLOAD); pubmsg.qos = QOS; pubmsg.retained = 0; MQTTClient_publishMessage(client,TOPIC,&pubmsg,&token); printf("Waiting for up to %d seconds for publication of %s\n" "on topic %s for client with ClientID: %s\n",(int)(TIMEOUT/1000),PAYLOAD,CLIENTID); rc = MQTTClient_waitForCompletion(client,token,TIMEOUT); printf("Message with delivery token %d delivered\n",token); MQTTClient_disconnect(client,10000); MQTTClient_destroy(&client); return rc; }
异步sub:
#include "stdio.h" #include "stdlib.h" #include "string.h" #include "MQTTClient.h" #define ADDRESS "tcp://192.168.1.103:1883" #define CLIENTID "ExampleClientSub" #define TOPIC "MQTT Examples" #define PAYLOAD "Hello World!" #define QOS 1 #define TIMEOUT 10000L 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,NULL); conn_opts.keepAliveInterval = 20; conn_opts.cleansession = 1; MQTTClient_setCallbacks(client,NULL,connlost,msgarrvd,delivered); if ((rc = MQTTClient_connect(client,rc); exit(EXIT_FAILURE); } printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n" "Press Q<Enter> to quit\n\n",QOS); MQTTClient_subscribe(client,QOS); do { ch = getchar(); } while(ch!='Q' && ch != 'q'); MQTTClient_disconnect(client,10000); MQTTClient_destroy(&client); return rc; }
makefile:
CC :=gcc SRC :=./src/sub.c FLAG := -I./include -L./lib LDFLAGS := -lpaho-mqtt3c all: ${CC} ${FLAG} ${SRC} -g -o sub ${LDFLAGS} .PYTHON:clean clean: -rm -rf demo1 *.o
开启windows上的mosquito:dos下进入mosquitto的安装目录,执行:mosquitto -c mosquitto.conf
上图: