Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

In a C# application I use EasyModbus to connect to a remote server:

ModbusClient client = new ModbusClient()
    IPAddress = "192.168.1.100",
    Port = 502,
    ConnectionTimeout = 200
    client.Connect();
catch (EasyModbus.Exceptions.ConnectionException)
    throw;
catch (System.Net.Sockets.SocketException)
    throw;

then in other parts of my application I try to read/write only if the client is still connected:

if (client.Connected)
        int[] readHoldingRegisters = client.ReadHoldingRegisters(1000, 10);
        // do something
    catch (Exception)
        throw;

If while the application is running I disconnect my laptop from the WiFi network, the Connected property is still true even after a long time. I get a "out of range" exception when I try to read the registers, but I wonder why that property doesn't turn to false immediately. At least, I would expect a "timeout" exception when I try to read/write something.

Is there a better approach to know the server is not reachable anymore?

It is actually not possible to detect a connection loss if the server has simply been disconnected without closing the connection. Then the Connected property is still true.

Maybe the Available method could be helpful. That method simply polls the server if it is still alive.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.