How to check internet connection in Qt



  • Hi all,,
    @bool networkInterface::networkAvailable()
    {
    QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
    bool result = false;

    for (int i = 0; i < ifaces.count(); i++)
    {
        QNetworkInterface iface = ifaces.at(i);
        if ( iface.flags().testFlag(QNetworkInterface::IsUp)
             && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) )
        {
    
            for (int j=0; j<iface.addressEntries().count(); j++)
            {
                if (result == false)
                    result = true;
            }
        }
    
    }
    return result;
    

    }
    @

    this code is checking internet and working correctly in windows,,,but if i disconnect the internet in ubuntu means still the online status is true,,Please give me a suggesstion,,
    Thanks,,



  • Hi saravanavelu39, How are you friend? :)
    I have a simple solution :)) very simple!!!
    just ping 8.8.8.8 ip and check output...
    Look at this:
    @QProcess pingProcess;
    QString exec = "ping";
    QStringList params;
    params << "-c" << "1" << IP;
    pingProcess.start(exec,params,QIODevice::ReadOnly);
    pingProcess.waitForFinished(-1);
    QString p_stdout = pingProcess.readAllStandardOutput();
    QString p_stderr = pingProcess.readAllStandardError();@



  • Hi Jafar,,
    Thanks for your reply,,,I used your code,,that is working correctly in ubuntu,,but the application some time hangs after using this code,,so could you please give me a some other suggesstion,,

    Thanks,



  • of course saravanavelu..
    There are some solution in this link
    i hope be useful :)
    http://ubuntuforums.org/showthread.php?t=1803980


Log in to reply
 

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