Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[mosquitto-dev] how to use mosquitto_connect and reconnect

Hi,

Let's say I have a loop that attempts to connect to a server:

while( ! connected){
   int rc = mosquitto_connect(...);
   if( <success> ) break;
   <delay and loop>
}

Is the above the correct way?

Let's say the server is unavailable, after a while the loop above will produce the error "Warning: Unable to open socket pair, outgoing publish commands may be delayed."

I saw the mosquitto source code, and realised connect_init() is opening a socketpair for every call, and does not close the socketpair. So after a while the OS runs out of file descriptors.

I think the below should be the correct way instead?

 int rc = mosquitto_connect(...);
bool connected = rc == SUCCESS;

while( ! connected){
   int rc = mosquitto_reconnect(...);
   if( <success> ) break;
   <delay and loop>
}

Please advise.

Back to the top