Linux下学习用C语言实现MQTT(三)(异步函数)

客户端库支持两种操作模式,称为同步模式和异步模式。如果你的应用程序调用了MQTTClient_setCallbacks(),则客户端将会进入异步模式,否则会以同步模式运行。

同步模式下,客户端应用程序运行在单个线程上。信息的发布使用MQTTClient_publish()和MQTTClient_publishMessage()函数。为了确定Qos1和Qos2(详见Quality of service)的信息已经成功传递,应用程序必须调用MQTTClient_waitForCompletion()这个函数。一个展示同步发布的例子可查看Synchronous publication example。同步模式下接收信息使用 MQTTClient_receive()函数,客户端应用程序必须相对频繁地调用 MQTTClient_receive()或MQTTClient_yield(),以便允许(执行)确认和保持与服务器的网络连接的 MQTT "ping"这些操作。

异步模式下,客户端应用程序运行在若干个线程上。如同同步模式,(异步模式中)主程序调用客户端库中的函数来发布和订阅,而在在后台进行握手和维持网络连接这些操作。客户端应用程序通过调用 MQTTClient_setCallbacks()( (详情请见MQTTClient_messageArrived(), MQTTClient_connectionLost()和 MQTTClient_deliveryComplete()))来使用库进行回调注册而获得状态通知和消息接收。然而, 这个 API 不是线程安全的-不可能从多个线程调用它而不进行同步。你可以使用MQTTAsync API来完成此操作。

接下使用MQTTAsync系列函数来实现publish和subscribe:
publish端:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#include <unistd.h>

#define ADDRESS     "tcp://localhost:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

volatile MQTTAsync_token deliveredtoken;

int finished = 0;

void connlost(void *context, char *cause)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
	int rc;

	printf("\nConnection lost\n");
	printf("     cause: %s\n", cause);

	printf("Reconnecting\n");
	conn_opts.keepAliveInterval = 20;
	conn_opts.cleansession = 1;
	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start connect, return code %d\n", rc);
 		finished = 1;
	}
}


void onDisconnect(void* context, MQTTAsync_successData* response)
{
	printf("Successful disconnection\n");
	finished = 1;
}


void onSend(void* context, MQTTAsync_successData* response)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
	int rc;

	printf("Message with token value %d delivery confirmed\n", response->token);

	opts.onSuccess = onDisconnect;
	opts.context = client;

	if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start sendMessage, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}
}


void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
	printf("Connect failed, rc %d\n", response ? response->code : 0);
	finished = 1;
}


void onConnect(void* context, MQTTAsync_successData* response)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
	MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
	int rc;

	printf("Successful connection\n");
	
	opts.onSuccess = onSend;
	opts.context = client;

	pubmsg.payload = PAYLOAD;
	pubmsg.payloadlen = (int)strlen(PAYLOAD);
	pubmsg.qos = QOS;
	pubmsg.retained = 0;
	deliveredtoken = 0;

	if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start sendMessage, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}
}


int main(int argc, char* argv[])
{
	MQTTAsync client;
	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
	int rc;

	MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);

	MQTTAsync_setCallbacks(client, NULL, connlost, NULL, NULL);

	conn_opts.keepAliveInterval = 20;
	conn_opts.cleansession = 1;
	conn_opts.onSuccess = onConnect;
	conn_opts.onFailure = onConnectFailure;
	conn_opts.context = client;
	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start connect, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}

	printf("Waiting for publication of %s\n"
         "on topic %s for client with ClientID: %s\n",
         PAYLOAD, TOPIC, CLIENTID);
	while (!finished)
		#if defined(WIN32)
			Sleep(100);
		#else
			usleep(10000L);
		#endif

	MQTTAsync_destroy(&client);
 	return rc;
}
  

subscribe端:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#include <unistd.h>


#define ADDRESS     "tcp://localhost:1883"
#define CLIENTID    "ExampleClientSub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

volatile MQTTAsync_token deliveredtoken;

int disc_finished = 0;
int subscribed = 0;
int finished = 0;

void connlost(void *context, char *cause)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
	int rc;

	printf("\nConnection lost\n");
	if (cause)
		printf("     cause: %s\n", cause);

	printf("Reconnecting\n");
	conn_opts.keepAliveInterval = 20;
	conn_opts.cleansession = 1;
	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start connect, return code %d\n", rc);
		finished = 1;
	}
}


int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_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');
    MQTTAsync_freeMessage(&message);
    MQTTAsync_free(topicName);
    return 1;
}


void onDisconnect(void* context, MQTTAsync_successData* response)
{
	printf("Successful disconnection\n");
	disc_finished = 1;
}


void onSubscribe(void* context, MQTTAsync_successData* response)
{
	printf("Subscribe succeeded\n");
	subscribed = 1;
}

void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
{
	printf("Subscribe failed, rc %d\n", response ? response->code : 0);
	finished = 1;
}


void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
	printf("Connect failed, rc %d\n", response ? response->code : 0);
	finished = 1;
}


void onConnect(void* context, MQTTAsync_successData* response)
{
	MQTTAsync client = (MQTTAsync)context;
	MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
	int rc;

	printf("Successful connection\n");

	printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
           "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
	opts.onSuccess = onSubscribe;
	opts.onFailure = onSubscribeFailure;
	opts.context = client;

	deliveredtoken = 0;

	if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start subscribe, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}
}


int main(int argc, char* argv[])
{
	MQTTAsync client;
	MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
	MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
	int rc;
	int ch;

	MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);

	MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL);

	conn_opts.keepAliveInterval = 20;
	conn_opts.cleansession = 1;
	conn_opts.onSuccess = onConnect;
	conn_opts.onFailure = onConnectFailure;
	conn_opts.context = client;
	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start connect, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}

	while	(!subscribed)
		#if defined(WIN32)
			Sleep(100);
		#else
			usleep(10000L);
		#endif

	if (finished)
		goto exit;

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

	disc_opts.onSuccess = onDisconnect;
	if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
	{
		printf("Failed to start disconnect, return code %d\n", rc);
		exit(EXIT_FAILURE);
	}
 	while	(!disc_finished)
		#if defined(WIN32)
			Sleep(100);
		#else
			usleep(10000L);
		#endif

exit:
	MQTTAsync_destroy(&client);
 	return rc;
}

编译同样要加上头文件,动态库,运行时可能也需要导入环境变量。

zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_publish 
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Successful connection
Message with token value 1 delivery confirmed
Successful disconnection
zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_subscribe 
Successful connection
Subscribing to topic MQTT Examples
for client ExampleClientSub using QoS1

Press Q<Enter> to quit

Subscribe succeeded
Message arrived
     topic: MQTT Examples
   message: Hello World!

客户端成功推送话题和订阅话题。
使用mosquitto分别测试publish和subscribe成功

zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_publish 
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Successful connection
Message with token value 1 delivery confirmed
Successful disconnection
zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ mosquitto_sub  -h 127.0.0.1 -p 1883 -v -t "MQTT Examples"
MQTT Examples Hello World!
zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ mosquitto_pub  -h 127.0.0.1 -p 1883 -t "MQTT Examples" -m "Hello World"
zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_subscribe 
Successful connection
Subscribing to topic MQTT Examples
for client ExampleClientSub using QoS1

Press Q<Enter> to quit

Subscribe succeeded
Message arrived
     topic: MQTT Examples
   message: Hello World

使用mqtt.fx应用程序测试软件成功
使用mqtt.fx订阅话题:

zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_publish 
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Successful connection
Message with token value 1 delivery confirmed
Successful disconnection

在这里插入图片描述
使用mqtt.fx推送话题:
在这里插入图片描述

zhanghang@Ubuntu-14:~/MQTT/paho.mqtt.c/build/output/samples$ ./MQTTAsync_subscribe 
Successful connection
Subscribing to topic MQTT Examples
for client ExampleClientSub using QoS1

Press Q<Enter> to quit

Subscribe succeeded
Message arrived
     topic: MQTT Examples
   message: hello
  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要在Linux系统下使用C语言实现MQTT客户端程序,可以按照以下步骤进行: 1. 首先,需要安装用于MQTT通信的C语言库。常见的MQTT库有Paho MQTT C库和Mosquitto MQTT C库,你可以选择其中一个进行安装。 2. 安装完成后,创建一个新的C文件,用于编写MQTT客户端程序的代码。 3. 在程序中包含MQTT库的头文件,并进行必要的初始化。例如,使用Paho MQTT C库,可以包含`"MQTTClient.h"`头文件,并通过`"MQTTClient_create()"`函数创建一个MQTT客户端实例。 4. 设置MQTT客户端连接的服务器地址和端口号。这可以通过使用`"MQTTClient_setOptions()"`函数来完成。 5. 定义一个回调函数,用于处理从MQTT服务器接收到的消息。例如,使用Paho MQTT C库,可以使用`"MessageArrived()"`函数来处理接收到的消息,并将其打印出来。 6. 使用`"MQTTClient_connect()"`函数连接到MQTT服务器。 7. 使用`"MQTTClient_subscribe()"`函数订阅你感兴趣的主题。 8. 实现发送MQTT消息的功能。可以使用`"MQTTClient_publish()"`函数发送消息到MQTT服务器。 9. 最后,使用`"MQTTClient_disconnect()"`函数断开与MQTT服务器的连接,并进行必要的资源清理。 10. 编译并运行你的程序。 总之,要在Linux系统下使用C语言实现MQTT客户端程序,你需要选择并安装一个MQTT库,创建一个新的C文件并编写相关代码,包括初始化、连接服务器、订阅主题、发送和接收消息等功能。最后,编译并运行你的程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值