简体   繁体   中英

C++ decrease modbus_connect timeout

I'd like to try 10 immediate modbus connections. However, everytime I fail to connect, and I have to wait for 2 minutes for the next connection because the previous modbus_connect call is still actively listening. So, if I fail to connect 10 times, I have to wait for 20 minutes.

int max_tries = 10;
int retries = 0;
while ((modbus_connect(ctx) == -1) && retries < max_retries){
    retries++;
    // wait 2 mins
    // I need to remove this waiting time
}

Can someone help me to reduce the time for timeout? I'm using Libmodbus v3.1.6

If you are talking about TCP connections, the behavior of your program may be correct.

There are several things involved here because you say you establish the connection and "is actively listening." Can't be both.

If you establish the connection, the only thing I can think of is that normally the connect (low level, not modbus) will try several times (after connecting) to send SYN packets (more or less two minutes) and if there is no response drop the connection.

That may be one problem.

If you are listening, you have to set the SO_REUSEADDR socket option.

In any case, you should verify errno and get the error description to know what is happening to your connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM