Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[mosquitto-dev] mosquitto client publish success, but server does not receive data ?

Greetings,

After a bit of a tussle with the client code, mosquitto_publish()
apparently states a successful publish.

But unfortunately, the server does not receive the data, but states
Socket error on client.
It is a bit bewildering to me that the data got published, but the
server did not receive any.

Any idea, what I am incorrectly doing ?
(Attached the code, my mail client messes up plain text emails ..)

Thanks,

Manu



On the client side, I see thus:

...
Mosquitto lib initialized, ret: 0
New Mosquitto Session instantiated
Dont verify Server Hostname in Certificate ...
16 Client mosq-Rd9dsJ1bWTrYF7cjnE sending CONNECT
Connected to Broker:  192.168.1.34 : 8883
16 Client mosq-Rd9dsJ1bWTrYF7cjnE sending PUBLISH (d0, q2, r0, m1,
'Testing', ... (12 bytes))
Publish success
16:39:20: D:\Work\Test
Apps\Qt_Mosquitto\build-QMosquitto-Desktop_Qt_5_15_0_MSVC2019_64bit-Debug\debug\QMosquitto.exe
exited with code 0

But on the server side all I see:

PS C:\Program Files\mosquitto> .\mosquitto.exe -c .\mosquitto.conf -v
1599044951: mosquitto version 1.6.10 starting
1599044951: Config loaded from .\mosquitto.conf.
1599044951: Opening ipv6 listen socket on port 8883.
1599044951: Opening ipv4 listen socket on port 8883.
1599044954: New connection from 192.168.1.34 on port 8883.
1599044954: Socket error on client <unknown>, disconnecting.
1599045521: mosquitto version 1.6.10 terminating
PS C:\Program Files\mosquitto>
#include <mosquitto.h>
#include <QString>
#include <QDebug>

struct mosquitto *mosq = NULL;

const char *CA_CERT	= "C:/Program Files/mosquitto/certs_v4/ca.crt";
const char *CLIENT_CRT	= "C:/Program Files/mosquitto/certs_v4/client.crt";
const char *CLIENT_KEY	= "C:/Program Files/mosquitto/certs_v4/client.key";

const char *MQTT_BROKER	= "192.168.1.34";
const char *MQTT_TOPIC	= "Testing";

int MQTT_PORT = 8883;
int keepalive = 60;

bool clean_session = true;


void mosq_log_callback(struct mosquitto *mosq, void *data, int level, const char *str)
{
	(void) mosq;
	(void) data;
	(void) level;

	qDebug() << level << str;
}

int publish(void)
{
	int ret;

	ret = mosquitto_lib_init();
	if (ret != MOSQ_ERR_SUCCESS) {
		qDebug() << "ERROR: mosquitto_lib_init, ret:" << ret << mosquitto_strerror(ret);
		return ret;
	}
	qDebug() << "Mosquitto lib initialized, ret:" << ret;

	mosq = mosquitto_new(NULL, clean_session, NULL);
	if (!mosq) {
		qDebug() << " ERROR: Out of Memory";
		return -1;
	}
	qDebug() << "New Mosquitto Session instantiated";
	mosquitto_log_callback_set(mosq, mosq_log_callback);

	qDebug() << "Dont verify Server Hostname in Certificate ...";
	ret = mosquitto_tls_insecure_set(mosq, true);	// dont verify hostname
	if (ret != MOSQ_ERR_SUCCESS) {
		qDebug() << "ERROR: mosquitto_tls_insecure_set, ret: " << ret << ", " << mosquitto_strerror(ret);
		return ret;
	}

	ret = mosquitto_tls_set(mosq,			// mosquitto object
				CA_CERT,		// cafile (CA certificate)
				NULL,			// capath
				NULL,			// certfile
				NULL,			// keyfile
				NULL);

	if (ret != MOSQ_ERR_SUCCESS) {
		qDebug() << "ERROR: moquitto_tls_set, ret: " << ret << ", " << mosquitto_strerror(ret);
		return ret;
	}

	ret = mosquitto_tls_opts_set(mosq,		// mosquitto object
				     0,			// 0=Verify NONE, 1=Verify SRV cert
				     "tlsv1.2",		// TLS Version to use
				     NULL);		// Use default cipher

	if (ret != MOSQ_ERR_SUCCESS) {
		qDebug() << "ERROR: mosquitto_tls_opts_set, ret: " << ret;
		return ret;
	}

	ret = mosquitto_connect(mosq, MQTT_BROKER, MQTT_PORT, keepalive);
	if (ret != MOSQ_ERR_SUCCESS) {
		qDebug() << "ERROR: Connecting to Broker, ret:" << ret << mosquitto_strerror(ret);
		return ret;
	}
	qDebug() << "Connected to Broker: " << MQTT_BROKER << ":" << MQTT_PORT;
	ret = mosquitto_publish(mosq, NULL, MQTT_TOPIC, sizeof("hello world"), "hello world", 2, false);
	if (ret != MOSQ_ERR_SUCCESS) {
		qDebug() << "ERROR: Mosquitto publish, ret:" << ret << mosquitto_strerror(ret);
		return ret;
	}

	ret = mosquitto_loop(mosq, 5000, 1);		// attempt to send any remaining messages
	if (ret != MOSQ_ERR_SUCCESS) {
		qDebug() << "ERROR: mosquitto_loop, ret: " << ret << mosquitto_strerror(ret);
		return ret;
	}
	qDebug() << "Publish success";

	mosquitto_lib_cleanup();
	return 0;
}

Back to the top