Skip to main content

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

Hi,

> 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.

Agreed this is a bug, it should be closing the old socketpair (or just
not creating a new pair) when it reinitialises.

> 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>
> }

Yes that should get around the file descriptor problem.
mosquitto_connect() should work the way you were using it, I'll get
that fixed.

As a more general point, both mosquitto_loop_start() and
mosquitto_loop_forever() carry out reconnecting for you. Unless you've
got a specific reason to be using mosquitto_loop() (integrating into
an existing event loop is the obvious reason) then I'd suggest you use
one of those two functions.

Cheers,

Roger


Back to the top