How to check whether the computer connects the internet?
-
hi Brother,look this:
void ThreadObject::pingNetwork(QString remotIP)
{
QProcess cmd; //类似于window里面的cmd命令
QTextCodec *codec = QTextCodec::codecForName("GBK");
QString outstr;QString pingString="ping "+ remotIP + " -n 1"; cmd.start(pingString); //ping ip地址 -n 1表示只ping一次就结束 cmd.waitForFinished(-1); //等待ping完成 outstr = codec->toUnicode(cmd.readAll()); //将ping之后的结果全部读到outstr中 if(-1!=outstr.indexOf("往返行程的估计时间")) //通过判断有无“往返行程的估计时间”这几个字来检测网路是否通 { qDebug() << "connect success!"; emit signal_networkstate(true); //发送网络连接的信号 } else { qDebug() << "connect failed!"; emit signal_networkstate(false); //发送网络断开的信号 }
}
just change the URL to any one you like. thank you
-
hi Brother,look this:
void ThreadObject::pingNetwork(QString remotIP)
{
QProcess cmd; //类似于window里面的cmd命令
QTextCodec *codec = QTextCodec::codecForName("GBK");
QString outstr;QString pingString="ping "+ remotIP + " -n 1"; cmd.start(pingString); //ping ip地址 -n 1表示只ping一次就结束 cmd.waitForFinished(-1); //等待ping完成 outstr = codec->toUnicode(cmd.readAll()); //将ping之后的结果全部读到outstr中 if(-1!=outstr.indexOf("往返行程的估计时间")) //通过判断有无“往返行程的估计时间”这几个字来检测网路是否通 { qDebug() << "connect success!"; emit signal_networkstate(true); //发送网络连接的信号 } else { qDebug() << "connect failed!"; emit signal_networkstate(false); //发送网络断开的信号 }
}
just change the URL to any one you like. thank you
-
Ummm, why do you need to check for internet connection?
Why not just see if the resource you are looking for is accessible?
Say you have to access "myawesomedomain.com".
Then try to access that domain. Maybe use an api:
myawesomedomain.com/status
If this access fails you tell the user that the resource is not accessible. Please check your internet connectivity. They can usually figure out its a connection problem. I would make the status separate from any services you use on the domain. That way you can tell in your app if a server on your domain is down, or if the network connection is not good.However if the check you use fails. Say checking for internet connection. And there is nothing wrong with their internet connection. They will think there is something wrong with your app.
-
fcarney is right -- What's the underlying question? Trying to determine if you are connected to the Internet is almost never useful information, and it's almost impossible to define well.
Is there a particular service you want to connect to? Just try to connect to that. Don't check if there's an internet connection an then only try to connect to the service if you think there's an Internet connection. You will drive users crazy when your Internet check fails despite having a route to your service. And when things work, it will be slower for no reason while you spend time trying to convince yourself there is an Internet connection.
-
Yes You Can also use
Ping
Like this
if(QProcess::execute("ping -n 1 www.google.com") == 0) { // You Are Connected With The Internet. } else { // You Are Not Connected With The Internet. }
@Ketan__Patel__0011
Hi.
For connecting my system i use websocketpp
I used the code below it works.
Why when my Beagle board lose connection (internet) to the server does not reconnect again.
I need all the time to reboot the system !!
There's an idea to reconnect the system fastly after internet returned back ?void DBNetworkReachability::checkReachability() { m_WebCtrl->clearAccessCache(); QUrl qurl("http://www.telenor.no"); QNetworkRequest request(qurl); QNetworkReply* reply = m_WebCtrl->get(request); connect( reply, SIGNAL(finished()),this, SLOT(replyFinished()) ); // added QEventLoop loop; QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit())); // QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); if(!reply->isFinished()) loop.exec(); if(reply->bytesAvailable()){ qDebug() << "You are connected to the internet :)"; } else{ qDebug() << "You are not connected to the internet :("; qDebug() << "Reconnecting"; QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit())); } }
-
@Ketan__Patel__0011
Hi.
For connecting my system i use websocketpp
I used the code below it works.
Why when my Beagle board lose connection (internet) to the server does not reconnect again.
I need all the time to reboot the system !!
There's an idea to reconnect the system fastly after internet returned back ?void DBNetworkReachability::checkReachability() { m_WebCtrl->clearAccessCache(); QUrl qurl("http://www.telenor.no"); QNetworkRequest request(qurl); QNetworkReply* reply = m_WebCtrl->get(request); connect( reply, SIGNAL(finished()),this, SLOT(replyFinished()) ); // added QEventLoop loop; QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit())); // QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); if(!reply->isFinished()) loop.exec(); if(reply->bytesAvailable()){ qDebug() << "You are connected to the internet :)"; } else{ qDebug() << "You are not connected to the internet :("; qDebug() << "Reconnecting"; QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit())); } }
@said_oslo what do you mean exactly by "reconnect the system"? Is there any task that you must resume after reconnecting to internet? Do you need the application to stay on stand-by until internet returns?
-
This works fine for me and doesn't "wait for reply" when there's no internet connection:
bool internetConnected(void) { QTimer timer; timer.setInterval(3000); timer.setSingleShot(true); QEventLoop eventLoop; QObject::connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit); QDnsLookup lookup; QObject::connect(&lookup, &QDnsLookup::finished, &eventLoop, &QEventLoop::quit); lookup.setType(QDnsLookup::A); lookup.setName("dns.google.com"); lookup.lookup(); timer.start(); eventLoop.exec(); if(timer.remainingTime() == 0) { lookup.abort(); return false; } if(lookup.error() != QDnsLookup::NoError) return false; return true; }
Note that this function is synchronous, so it might freeze the UI for some time (0 - 3 seconds) if the connection is slow.
But seems to return immediately with a regular connection. -
@sierdzio
Thank you!
Yes, I want to know if computer is connected to Internet.To ping a server is also a solution I find on the Internet. But I don't know how to check the result with QT. Maybe I should have a try first.
This post is deleted! -
This works fine for me and doesn't "wait for reply" when there's no internet connection:
bool internetConnected(void) { QTimer timer; timer.setInterval(3000); timer.setSingleShot(true); QEventLoop eventLoop; QObject::connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit); QDnsLookup lookup; QObject::connect(&lookup, &QDnsLookup::finished, &eventLoop, &QEventLoop::quit); lookup.setType(QDnsLookup::A); lookup.setName("dns.google.com"); lookup.lookup(); timer.start(); eventLoop.exec(); if(timer.remainingTime() == 0) { lookup.abort(); return false; } if(lookup.error() != QDnsLookup::NoError) return false; return true; }
Note that this function is synchronous, so it might freeze the UI for some time (0 - 3 seconds) if the connection is slow.
But seems to return immediately with a regular connection.@adazem009 Why are you responding to a two year old thread?
-
@adazem009 Why are you responding to a two year old thread?
@wrosecrans Just in case someone has the same problem. I actually found this thread when I was looking for a solution.
-
@wrosecrans Just in case someone has the same problem. I actually found this thread when I was looking for a solution.
Some variant of this question surfaces every few months, almost always initiated by someone who really does not understand what the internet is. InterNet in itself is an ambiguous term. It is a network of interconnected networks. There is no internet point of authority to ping with and come to the conclusion "I can reach the internet". It is by nature designed so that some segments may be reached even if other large portions are down. So, you need to be very specific with your intentions and expectations when branching outside of your LAN (local area network).