How to check internet connection in Qt
-
wrote on 9 Sept 2014, 09:43 last edited by
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,, -
wrote on 9 Sept 2014, 15:13 last edited by
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();@ -
wrote on 24 Sept 2014, 12:18 last edited by
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,
-
wrote on 25 Sept 2014, 06:03 last edited by
of course saravanavelu..
There are some solution in this link
i hope be useful :)
http://ubuntuforums.org/showthread.php?t=1803980