Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Check if machine has access to the internet



  • 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?



  • 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.



  • 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".


  • Lifetime Qt Champion

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


Log in to reply