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

Check internet connection in Linux



  • I need a reliable way to check for internet availability in linux using standard C++ or Qt C++. I thought that would be a trivial task but I searched everywhere and all the solutions I found was some kind of hack using ping or curl.

    Ping was the most common solution I found but I don't want to use it because it requires to ping a host and that's not reliable since the program could be running in a country that blocks that host, like google.com in China.

    I have, however, found a way to do check internet connection in Windows here with this code:

    bool ReachabilityCheck::internetAvailable()
    {
        bool result = true;
    
        DWORD lpresult;
        BOOL winresult = InternetGetConnectedState(&lpresult,0);
        result = winresult == 1 ? true : false;
    
        return result;
    }
    

    And now I am looking for a way to to achieve that in a linux environment.

    Any help or insight is appreciated! Thanks!



  • @hbatalha
    This has been asked before, and the conclusion was: nothing better than trying to connect to google.com, or whatever equivalent for you.



  • This is more a philosophical than a programming question.
    From the technical point of view the internet is just a network.
    So how do you define "the internet".
    If you say there has to be some webpage available then per that definition there is no internet in countries where it its blocked but some local other net.

    If you need the information to know if you can contact a server then just directly try to contact it or what is the purpose of knowing if there is some internet around?


Log in to reply