官网: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
编译过程中报找不到clock_gettime函数的错误,在Makefile中相关位置添加-lrt就可以了。
make后sample里的测试代码也会跟着成功编译。
也可以在sample目录下单独用命令编译:gcc -I../ -L../../../paho.mqtt.c/build/output/ MQTTClient_subscribe.c -lpaho-mqtt3c -o MQTTTest -lrt
我测试了订阅部分,修改了原版demo,同时和服务器建立4个连接,每个连接又订阅了4个主题。通过对主题进行数据发送和抓包分析,都显示没问题。
修改后的代码如下:
/*******************************************************************************
* 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;
}