Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Mosquitto » synchronous(why the process is not blocked, when I use synchronous. )
synchronous [message #1838947] Wed, 10 March 2021 08:22
muqali He is currently offline muqali HeFriend
Messages: 1
Registered: March 2021
Junior Member
my code is
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "MQTTClient.h"

#define SERVERURL	"tcp://localhost:1883"
#define CLIENTID	"clientPub"
#define TOPIC		"synchronous"
#define PAYLOAD	"Hello, Synchronous!"
#define QOS		2
#define TIMEOUT	10000L


int main( int argc, char* argv[] )
{
	MQTTClient client ;
	MQTTClient_connectOptions connOpts = MQTTClient_connectOptions_initializer ;
	MQTTClient_message pubmsg = MQTTClient_message_initializer ;
	MQTTClient_deliveryToken token ;
	
	char buf[1024] ;	
	int rc ;
	
	// create a MQTTClient object
	MQTTClient_create(&client, SERVERURL, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL) ;
	
	// set connect options
	connOpts.keepAliveInterval = 20 ;
	connOpts.cleansession = 1 ;
	
	// try to connect server
	if( (rc = MQTTClient_connect(client, &connOpts)) != MQTTCLIENT_SUCCESS )
	{
		printf("ClientPub.c: fail to connect, error code is %d\n", rc) ;
		exit(EXIT_FAILURE) ;
	}
	
	while(1)
	{	
	
	        // publish message
	
	        printf("Input msg: \n") ;
	        scanf("%s", buf) ;
	
	        pubmsg.payload = buf ;
	        pubmsg.payloadlen = strlen(buf) ;
	        pubmsg.retained = 0 ;
	

	        MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token) ;
	
	        rc = MQTTClient_waitForCompletion(client, token, TIMEOUT/1000) ;
	        if(rc == MQTTCLIENT_SUCCESS)
	        {
		          printf("Message with delivery token %d delivered\n", token);
	         }
	}
	MQTTClient_disconnect(client, 10000);
    	MQTTClient_destroy(&client);
	return rc ;
}

The document indicates waitForCompletion() will be blocked when it called, but when client connect to server successfully, I close server. why the process isn't blocked in waitForCompletion() ?


And I want to know in synchronous mode where the blocking will be happen ?


thank u very much.
Previous Topic:Mosquitto software design
Next Topic:Mosquitto Plugin
Goto Forum:
  


Current Time: Fri May 03 01:32:19 GMT 2024

Powered by FUDForum. Page generated in 0.02660 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top