加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

mosquitto(windows)作为broker,paho作为client

发布时间:2020-12-13 21:04:31 所属栏目:Windows 来源:网络整理
导读:环境: client :ubuntu broker:windows 32bit 下载mosquitto 的exe版本,直接安装运行,如果不过,请自行上网搜索安装过程以及解决办法; 本文主要讲解paho的demo过程,而且还是参考其他人的博客,博客地址:https://blog.csdn.net/qingdujun/article/details

环境: 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

3.参考上边大神的博客,直接用几个函数搞定:

同步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 %dn",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 %sn"
            "on topic %s for client with ClientID: %sn",(int)(TIMEOUT/1000),PAYLOAD,CLIENTID);
    rc = MQTTClient_waitForCompletion(client,token,TIMEOUT);
    printf("Message with delivery token %d deliveredn",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 confirmedn",dt);
    deliveredtoken = dt;
}
int msgarrvd(void *context,char *topicName,int topicLen,MQTTClient_message *message)
{
    int i;
    char* payloadptr;
    printf("Message arrivedn");
    printf("     topic: %sn",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 lostn");
    printf("     cause: %sn",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 %snfor client %s using QoS%dnn"
           "Press Q<Enter> to quitnn",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

上图:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读