← Back to team overview

modbus team mailing list archive

Little modbus server

 

Hello,

To make simulations, I based the code of a little server on one of the
libmodbus test case. In those tests, when the client exits, the server stops
too. For convenience, I tried to change this behavior and obtained this main
loop :
    for (;;) {
    uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
    int rc;

    rc = modbus_receive(ctx, query);
    if (rc != -1) {
        /* rc is the query size */
        for (list<Voie *>::iterator it = lst.begin() ;
            it != lst.end(); it++) {
        (*it)->getValue(mb_mapping->tab_registers);
        }

        modbus_reply(ctx, query, rc, mb_mapping);
    } else {
        /* Connection closed by the client or error */
        //break;
    }
    }

As you can see, I just removed the //break// call.

My difficulty arrives when a new client connects. It gets many errors of
type "Connection timed out". If I stop the server and restart it, this error
doesn't appear.

What's the best solution to allow a good reconnection ?

Thanks.
David.