PDA

View Full Version : Check network connection



sophister
6th November 2009, 06:14
Hello:
I want to write a application that can record the time my computer's network connection is activited (i.e, I can surf the Internet with it) and the time it becomes disconnected.

I use the QNetworkAccessManager::get(QNetworkRequest&) to test the network connection. But the question is, I am thinking that it is not the best way or right way to test network connection using that method.

Can you give me some advice?
Thanks a lot!

yogeshgokul
6th November 2009, 07:19
I'd suggest you to use
socket->connectToHost("hostName", portNumber);
if (socket->waitForConnected(1000))
qDebug("Connected!");.
In this method, you can try connecting to a valid host to a period of time and if it succeeds then fine otherwise time out will occur. The value of trying time is in your hands.

squidge
6th November 2009, 09:00
The best way is probably a ping request, which Qt doesn't seem to support, plus on some OSs it requires higher priviledges as it needs raw sockets.

Best Qt way is to try and connect to a host and see if it works.

yogeshgokul
9th November 2009, 06:26
The best way is probably a ping request, which Qt doesn't seem to support, Then why you are mentioning it here.;)

squidge
22nd November 2009, 11:32
As it's still the best, even if Qt doesn't support it. Plus, maybe someone will create a class to do and submit it to Nokia if they think it'll be useful.

Typically, if you talk about something that isn't possible regularly enough, it becomes a wanted feature, and gets implemented :)

If we only ever talk about things which are possible, then the toolkit will never get extended or enhanced, as people will think it supports everything you could ever want.

wysota
22nd November 2009, 11:54
As it's still the best, even if Qt doesn't support it. Plus, maybe someone will create a class to do and submit it to Nokia if they think it'll be useful.

On most platforms you wouldn't be able to send an IMCP packet without superuser privileges so it would be hard to call that a portable solution which means it wouldn't go to Qt anyway. And on the remaining platforms it's just a few lines of code to implement it natively. Besides, there is always QProcess and the 'ping' command available on most platforms.

squidge
22nd November 2009, 16:59
If 'ping' and QProcess is available on most platforms, wouldn't you call such thing a solution which could be portable? There could be a class which does it natively on platforms which support it, and via QProcess for platforms that don't.

wysota
22nd November 2009, 18:37
If 'ping' and QProcess is available on most platforms, wouldn't you call such thing a solution which could be portable?

Not really. Qt doesn't do such things like using QProcess to access some other executable to do some job, then parse all possible kinds of output and return the result to the user. Besides, at least on some Linux distros ping requires you to have superuser access rights as well.

I would be more interested in seeing proper support for serial devices in Qt rather than a wrapper around ping. Which is completely unreliable as a method to check if you have a working network connection, by the way.

Hossein
3rd September 2012, 13:00
Hello guys.
How can i specify the connection type?
I want to check the VPN connections only. how can i do that in Qt? i have already written a code to check for Network Connectivity now all i need is to specify it to a VPN connection only.
Here is my code so far"

bool MainWindow::CheckNetworkConnectivity()
{
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
for(int i = 0; i < interfaces.count();i++)
{
QNetworkInterface interface = interfaces.at(i);
if(interface.IsUp && !interface.IsLoopBack)
{
ui->lstLog->addItem(interface.name()+QDateTime::currentDateTim e().toString("h:m:s ap"));
ui->chkConStatus->setChecked(true);
}
else{
ui->chkConStatus->setChecked(false);
}
}
return ui->chkConStatus->checkState();
}
Regards in advance :)
Hossein