Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpSocket: Detect network connection lost
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket: Detect network connection lost

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 12.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KA51O
    wrote on 19 Feb 2014, 12:50 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JKSH
      Moderators
      wrote on 20 Feb 2014, 03:38 last edited by
      #2

      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)

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ankursaxena
        wrote on 20 Feb 2014, 04:19 last edited by
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Seamus Boyle
          wrote on 20 Feb 2014, 04:30 last edited by
          #4

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

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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on 20 Feb 2014, 08:25 last edited by
            #5

            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]

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JKSH
              Moderators
              wrote on 20 Feb 2014, 11:05 last edited by
              #6

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

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on 20 Feb 2014, 12:01 last edited by
                #7

                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.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 20 Feb 2014, 12:09 last edited by
                  #8

                  [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 :)

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    blaroche
                    wrote on 7 Mar 2014, 21:31 last edited by
                    #9

                    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));@

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved