This forum has been archived. Please start a new discussion on GitHub.

How to find out if the connection is lost?


So, suppose a client application pings a server every 5 seconds to see whether it is running. After starting the server, the client application establishes a tcp-connection. But how do you know that the connection has been disconnected? Now the application continues to ping the server every 5 seconds, but can I get rid of it? Is there any way to find out about the state of a connection? Without a ping to the server.

Thank you!


  • I mean if I start a simple server and client and then close the server, I see the following message on the client-side:
    ==> -- Network: received 14 of 14 bytes via tcp
        -- Protocol: received close connection
        -- Network: closing tcp connection

    And if I forcibly close my server, I get the following message on the client-side:
    ==> -- warning: connection exception:
           TcpTransceiver.cpp:469: Ice::ConnectionLostException
        -- Network: closing tcp connection

    Thus a TraceUtil knows about shutting down the server, but how can I know this? How can I know that the connection is closed without any requests to the server?
  • xdm
    xdm La Coruña, Spain

    ping the server at regular intervals is the recommended approach, see Why does Ice not provide a callback to notify of connection closure?
  • "The TCP/IP stack doesn't provide reliable connection closure notifications"
    But what about keepalive packets or rst signals?
    "For example, a TCP/IP connection can be left in a half-open state where the peers won't detect the connection loss until they actually try to send data over the connection."
    But your trace util notifies of connection closure, as in the case of properly closing the connection (it prints "received close connection") and in the case of unexpected closure ("Ice::ConnectionLostException").

    And as far as I understand, this is done without a ping to the server.
    I mean Ice should provide not a callback but some function like isConnectionLost() returns true if connection was closed and it's the well-known via tcp without ping the server.
  • xdm
    xdm La Coruña, Spain
    Some times "Ice::ConnectionLostException" is only throw after you try to send data over the connection, the only reliable way to keep a connection open is to send packets this will allow you application to detect the connection lost in a timely manner.

    Why do you need this, what is your application trying to do?
  • Just have the task to minimize the traffic between the nodes the network. It was minimized as far as possible by high-level tools, and now I think how can I get rid of ping servers every N seconds.

    Many thanks for your answers!