The documentation says: * Important note * * The following functions that deal with network operations will return * MOSQ_ERR_SUCCESS on success, but this does not mean that the operation has * taken place. An attempt will be made to write the network data, but if the * socket is not available for writing at that time then the packet will not be * sent. To ensure the packet is sent, call mosquitto_loop() (which must also * be called to process incoming network data). * This is especially important when disconnecting a client that has a will. If * the broker does not receive the DISCONNECT command, it will assume that the * client has disconnected unexpectedly and send the will. * * mosquitto_connect() * mosquitto_disconnect() * mosquitto_subscribe() * mosquitto_unsubscribe() * mosquitto_publish() You haven't called one of the mosquitto_loop*() functions, so you will never receive a CONNACK. You've also not called mosquitto_lib_init(). If you want the publish to happen after a successful connection, put it in the on_connect() callback. If you want to disconnect after a successful publish, but that in the on_publish() callback and use a while loop to call mosquitto_loop() (or just use mosquitto_loop_forever()). On Tue, Dec 17, 2013 at 11:05 AM, Yun Wu wrote: > ** Description changed: > > It seems that blocking mode is not really supported. > > Topology: publisher -- broker -- subscriber > Publisher simply publish a message then exit. > Subscriber sometimes can receive message published by publisher, but sometimes not. > > The root cause is, libmosquitto does not really support blocking mode. > mosquitto_connect_bind --> _mosquitto_reconnect --> _mosquitto_send_connect > That means mosquitto_connect_bind() does not wait until CONNACK arrives. > > Logs can prove that: > ---- > 1387273535: New connection from 172.18.111.243 on port 1883. > 1387273535: New client connected from 172.18.111.243 as TestClientId (c1, k3600). > 1387273535: Sending CONNACK to TestClientId (0) > 1387273535: Socket error on client TestClientId, disconnecting. > ---- > We can see: > 1) Broker has not received PUBLISH message from publisher. > 2) Broker has not received DISCONNECT message from publisher. > > All because blocking mode is not really supported. > > - In blocking mode, > + In blocking mode, > mosquitto_connect_bind should wait until CONNACK arrives; > If QoS not 0, mosquitto_publish should wait unit ... > mosquitto_subscribe should wait unit ... > ... > + > + > + > + Here is code of the publisher: > + > + #include > + #include > + #include > + #include > + #include > + #include "mosquitto.h" > + > + #define dbg(fmt, args...) \ > + printf("%s(%u): "fmt"\n", __FUNCTION__, __LINE__, ##args) > + > + int main(int argc, char *argv[]) > + { > + struct mosquitto *mosq = NULL; > + int rv; > + const char topic[] = "TestTopic"; > + const char buf[] = "Hello, world"; > + > + if (MOSQ_ERR_SUCCESS != mosquitto_lib_init()) { > + return -1; > + } > + > + if (NULL == (mosq = mosquitto_new("TestClientId", 1, NULL))) { > + dbg("mosquitto_new() failed"); > + rv = -1; > + goto quit; > + } > + > + rv = mosquitto_connect_bind(mosq, "172.18.111.245", 1883, 3600, "172.18.111.243"); > + if (MOSQ_ERR_SUCCESS != rv) { > + dbg("mosquitto_connect_bind() failed: %s", mosquitto_strerror(rv)); > + if (MOSQ_ERR_ERRNO == rv) { > + dbg("errno = %d: %s", errno, strerror(errno)); > + } > + rv = -1; > + goto quit; > + } > + > + if (MOSQ_ERR_SUCCESS != mosquitto_publish(mosq, NULL, topic, sizeof(buf), buf, 0, 0)) { > + dbg("mosquitto_publish to %s failed", topic); > + rv = -1; > + goto quit; > + } > + > + quit: > + if (NULL != mosq) { > + mosquitto_disconnect(mosq); > + mosquitto_destroy(mosq); > + } > + mosquitto_lib_cleanup(); > + return rv; > + } > > -- > You received this bug notification because you are subscribed to > mosquitto. > https://bugs.launchpad.net/bugs/1261683 > > Title: > Blocking mode is not really supported > > To manage notifications about this bug go to: > https://bugs.launchpad.net/mosquitto/+bug/1261683/+subscriptions