百度IOT Hub (11) Paho C Client Async Publish Operation without SSL
1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#if !defined(WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif
#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif
#define ADDRESS "tcp://sdmn79a.mqtt.iot.gz.baidubce.com:1883"
#define CLIENTID "MyMonitor_Pub"
#define TOPIC "$baidu/iot/general/abc"
#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("\n[connlost] : Connection lost\n");
printf(" cause: %s\n", cause);
printf("[connlost] : Reconnecting\n");
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("[connlost] : Failed to start connect, return code %d\n", rc);
finished = 1;
}
}
void onDisconnect(void* context, MQTTAsync_successData* response)
{
printf("[onDisconnect] : 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("[onSend] : 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("[onSend] : Failed to start sendMessage, return code %d\n", rc);
exit(EXIT_FAILURE);
}
}
void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
printf("[onConnectFailure] : 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("[onConnect] : 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("[onConnect] : 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;
conn_opts.username = "sdmn79a/MyMonitor_Pub";
conn_opts.password = "xxxxxxxxxxxxxx";
conn_opts.MQTTVersion = MQTTVERSION_3_1_1;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("[main] : Failed to start connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
printf("[main] : 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;
}
2.
- 点赞
- 收藏
- 分享
- 文章举报
- 百度IOT Hub (11) Paho C Client Async Publish Operation without SSL
- 百度IOT Hub (12) Paho C Client Async Subscribe Operation without SSL
- 百度IOT Hub (12) Paho C Client Async Subscribe Operation without SSL
- utilize HttpClient to generate a SSL access and generate REST access to fetch data, async programming? cool and brief
- 使用PHPMailer 中的报错解决 "Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:"
- ASP.NET Core 2.1 : 十三.httpClient.GetAsync 报SSL错误的问题
- javax.net.ssl.SSLException: hostname in certificate didn't match证书不匹配的主机名(使用的是AsyncHttpClient)
- 利用Native Client OLEDB 11 高效率地对SQL SERVER 进行查询和插入操作
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- SmartGwt: attempt to use DataSource of type iscServer without SmartClient Server option.
- SMTP Client with SSL/TLS
- 11_Android中HttpClient的应用,读取网络xml及xml解析流,Handler的应用,LayoutInflater的使用,SmartImageView的使用
- [HttpClient]SSL双向实例
- Java Client using FTP over SSL (Explicit)
- python爬虫(11)身边的搜索专家——获取百度搜索结果
- HttpAsyncClient之模拟post请求示例
- https应用:避免HttpClient的”javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”异常
- 文件上传 借助的是AsyncHttpClient
- MySQL警告:Establishing SSL connection without server's identity verification is not recommended. Accor
- Mac OS X 11中的/usr/bin 的“Operation not permitted”