1pubsync(3)                 Library Functions Manual                 pubsync(3)
2
3
4

NAME

6       pubsync - Synchronous publication example
7
8
9       #include <stdio.h>
10       #include <stdlib.h>
11       #include <string.h>
12       #include "MQTTClient.h"
13
14       #define ADDRESS     "tcp://mqtt.eclipse.org:1883"
15       #define CLIENTID    "ExampleClientPub"
16       #define TOPIC       "MQTT Examples"
17       #define PAYLOAD     "Hello World!"
18       #define QOS         1
19       #define TIMEOUT     10000L
20
21       int main(int argc, char* argv[])
22       {
23           MQTTClient client;
24           MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
25           MQTTClient_message pubmsg = MQTTClient_message_initializer;
26           MQTTClient_deliveryToken token;
27           int rc;
28
29           if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
30               MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
31           {
32                printf("Failed to create client, return code %d0, rc);
33                exit(EXIT_FAILURE);
34           }
35
36           conn_opts.keepAliveInterval = 20;
37           conn_opts.cleansession = 1;
38           if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
39           {
40               printf("Failed to connect, return code %d0, rc);
41               exit(EXIT_FAILURE);
42           }
43
44           pubmsg.payload = PAYLOAD;
45           pubmsg.payloadlen = (int)strlen(PAYLOAD);
46           pubmsg.qos = QOS;
47           pubmsg.retained = 0;
48           if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
49           {
50                printf("Failed to publish message, return code %d0, rc);
51                exit(EXIT_FAILURE);
52           }
53
54           printf("Waiting for up to %d seconds for publication of %s0
55                   "on topic %s for client with ClientID: %s0,
56                   (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
57           rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
58           printf("Message with delivery token %d delivered0, token);
59
60           if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
61               printf("Failed to disconnect, return code %d0, rc);
62           MQTTClient_destroy(&client);
63           return rc;
64       }
65
66
67Paho MQTT C Client Library      Sat May 29 2021                     pubsync(3)
Impressum