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. Check if machine has access to the internet
Forum Updated to NodeBB v4.3 + New Features

Check if machine has access to the internet

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 1.2k Views 2 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.
  • SeDiS Offline
    SeDiS Offline
    SeDi
    wrote on last edited by
    #1

    When trying to check the internet connection of the machine (Windows, Android, Linux) running my app, I've found two main ideas. First: to ping a server (like google). I don't want to do this, if there's a better solution. Second: to use QNetworkConfigurationManager::isOnline(). The latter, though, is deprecated.

    So I've tried this approach:

        Q_INVOKABLE bool isOnline() {
            bool result = false;
            auto interfaces = QNetworkInterface::allInterfaces();
            for (auto i: interfaces) {
                if (i.flags() & QNetworkInterface::IsRunning) return true;
            }
            return result;
        }
    

    Unfortunately, this returns true, even when I plug out the network cable (tested on Windows, Qt 5.15.2).
    Is there a good or best practice to quickly check the connection status?

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by Kent-Dorfman
      #2

      People unfortunately have too vague an idea what "access to the internet" means. To get a concrete answer you need to specify what that means to you.

      The InterNet is a conclomerate of interconnected networks that share the same addressing mechanisms and protocols. Wouldn't that means you need to send and receive some traffic to verify that access?

      As you've discovered above, an "up" interface means nothing, as an inteface can remain "up" even if it can't talk to the world. It stays "up" as long as the ethernet connection has link signal (layer-2) with another node, or switch. Even then there is no hard and fast rule about an interface dropping if the link is lost, since it staying active has some value if the connection is being relocated to another port on the switch.

      1 Reply Last reply
      4
      • SeDiS Offline
        SeDiS Offline
        SeDi
        wrote on last edited by SeDi
        #3

        I am definitively guilty of the above, sorry.
        Actually I have to be able reach one specific server to request the result of a barcode search.

        But given the fact that my users are most probably on a mobile device, I would like to warn, if there's no result due to a connection problem (no service, flight mode, etc.).
        Perhaps: "see a working DNS server" would be specific enough ;-)

        My problem is, that I just don't get notified, when a request does not come through.

        For now I use this code for the request:

           function request(url, callback) {
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = (function(myxhr) {
                    return function() {
                        if(xhr.readyState === XMLHttpRequest.DONE) {
                            var status = xhr.status;
                            if (status === 0 || (status >= 200 && status < 400)) {
                                //  The request has been completed successfully
                                callback(myxhr);
                            } else {
                                console.log("There has been an error with the XMLHttpRequest.")
                            }
                        }
                    }
                })(xhr);
                xhr.open('GET', url, true);
                xhr.send('');
            }
        

        And this code calls it:

                        scanEANPage.request('someserver.tld/somewhere/somefile.json', function (o) {
                            {
                                mainWindow.scannedObject = eval('{new Object(' + o.responseText + ');}');
                            }
                            controller.processContent(tag, o.responseText)
        
                        })
        

        As of now, I have never reached the output "There has been an error with the XMLHttpRequest".

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          So what value has
          var status
          when it connect get a result versus
          when it cant ?

          Also did you look into
          https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
          Seems you can use addEventListener to know about progress.
          (if that in fact the API you are using :)

          1 Reply Last reply
          1
          • M Offline
            M Offline
            markwooen12
            wrote on last edited by
            #5

            To check if a machine has access to the internet, you can use the ping command. This command sends an ICMP packet to a host and if a response is received then there is a connection between the two. You can also use the traceroute command to trace the route packets take to a specific host, which can be used to determine estimated download times if there is an internet connection.

            1 Reply Last reply
            0
            • Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by
              #6

              Hello!

              You can check the Internet connection using QTcpSocket (https://doc.qt.io/qt-6/qtcpsocket.html). Please, check out my code below:

              Code:

              bool checkInternetConnection()
              {
                  bool isConnected = false;
                  QTcpSocket checkConnectionSocket;
                  checkConnectionSocket.connectToHost("google.com", 443); // 443 for HTTPS or use Port 80 for HTTP
                  checkConnectionSocket.waitForConnected(2000);
              
                  if (checkConnectionSocket.state() == QTcpSocket::ConnectedState) {
                      isConnected = true;
                  }
              
                  checkConnectionSocket.close();
                  return isConnected;
              }
              

              Happy coding!

              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