QTcpSocket: Detect network connection lost



  • I want my QTcpSocket to detect when the network connection is lost (e.g. cable unplugged). In "this thread":http://qt-project.org/forums/viewthread/2420 i read that I have to periodically test the connection myself to notice the connection loss. I implemented this and of course it works, but then I found the QAbstractSocket::KeepAliveOption and thought this might be a better solution to my problem. So I set the socket option, but the socket still doesn't notice the loss of connection (I waited 5 minutes).
    @
    m_pTcpSocket = new QTcpSocket(this);
    m_pTcpSocket->connectToHost(m_sHost, m_uPort);
    m_pTcpSocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
    @

    Am I missing something or do I really have to implement the polling myself?


  • Moderators

    I believe the socket emits an error() signal when connection is lost. (I know it does if one side closes the connection properly, but I don't know what happens if the plug is pulled)



  • I think u should used a signal disconnected . QAbstractSocket::disconnected



  • One of these may help:
    @QNetworkConfigurationManager::onlineStateChanged()@

    @
    QNetworkAccessManager::activeConfiguration()
    QNetworkConfiguration::state() .testFlag(QNetworkConfiguration::Active)
    @



  • Thanks for all the suggestions.
    Unfortunatly the error() signal is not emitted when I pull the plug. I have been waiting for 30 minutes but neither error nor disconnect nor stateChanged were emitted. They are only emitted when I try to send something which then obviously fails.

    This is somewhat surprising since TCP is supposed to be reliable and connection-oriented and in combination with the KeepAliveOption should notice connection loss.

    Judging from the Microsoft docs on SO_KEEPALIVE option http://msdn.microsoft.com/de-de/library/windows/desktop/ee470551(v=vs.85).aspx I think the problem is the OS's default timeout interval:
    [quote]
    The SO_KEEPALIVE socket option is valid only for protocols that support the notion of keep-alive (connection-oriented protocols). For TCP, the default keep-alive timeout is 2 hours and the keep-alive interval is 1 second. The default number of keep-alive probes varies based on the version of Windows.
    [/quote]


  • Moderators

    I find that surprising too. Would you like to file a bug report?



  • The thing is, if the connection is lost in the "normal" way (one socket disconnects) everything works as intended. Also the behaviour is as intended when you read the OS specific documentation. I'm just missing a way to set the OS timeout interval via Qt i guess. Don't know if this can be considered a bug.


  • Moderators

    [quote author="KA51O" date="1392897670"]The thing is, if the connection is lost in the "normal" way (one socket disconnects) everything works as intended. Also the behaviour is as intended when you read the OS specific documentation. I'm just missing a way to set the OS timeout interval via Qt i guess. Don't know if this can be considered a bug.[/quote]I see. Well, it sounds like a reasonable feature request :)



  • I have been able to get around this by using the below. But I don't think this is the best solution and am looking for others.

    @int fd = socketDescriptor();
    int enableKeepAlive = 1;
    int maxIdle = 10;
    int count = 3;
    int interval = 2;
    int result;

    result = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enableKeepAlive, sizeof(enableKeepAlive));
    result = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &maxIdle, sizeof(maxIdle));
    result = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(count));
    result = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.