Results 1 to 9 of 9

Thread: Check network connection

  1. #1
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Check network connection

    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!

  2. The following user says thank you to sophister for this useful post:

    bunjee (22nd November 2009)

  3. #2
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Check network connection

    I'd suggest you to use
    Qt Code:
    1. socket->connectToHost("hostName", portNumber);
    2. if (socket->waitForConnected(1000))
    3. qDebug("Connected!");
    To copy to clipboard, switch view to plain text mode 
    .
    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.
    Last edited by yogeshgokul; 6th November 2009 at 08:59.

  4. The following user says thank you to yogeshgokul for this useful post:

    sophister (6th November 2009)

  5. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Check network connection

    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.

  6. The following user says thank you to squidge for this useful post:

    sophister (6th November 2009)

  7. #4
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Check network connection

    Quote Originally Posted by fatjuicymole View Post
    The best way is probably a ping request, which Qt doesn't seem to support,
    Then why you are mentioning it here.

  8. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Check network connection

    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.

  9. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Check network connection

    Quote Originally Posted by fatjuicymole View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Check network connection

    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.

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Check network connection

    Quote Originally Posted by fatjuicymole View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #9
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Thanks
    33
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Check network connection

    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"
    Qt Code:
    1. bool MainWindow::CheckNetworkConnectivity()
    2. {
    3. QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
    4. for(int i = 0; i < interfaces.count();i++)
    5. {
    6. QNetworkInterface interface = interfaces.at(i);
    7. if(interface.IsUp && !interface.IsLoopBack)
    8. {
    9. ui->lstLog->addItem(interface.name()+QDateTime::currentDateTime().toString("h:m:s ap"));
    10. ui->chkConStatus->setChecked(true);
    11. }
    12. else{
    13. ui->chkConStatus->setChecked(false);
    14. }
    15. }
    16. return ui->chkConStatus->checkState();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Regards in advance
    Hossein

Similar Threads

  1. Replies: 12
    Last Post: 22nd March 2009, 11:22
  2. QFtp with no network connection
    By josepvr in forum Qt Programming
    Replies: 0
    Last Post: 19th November 2008, 15:03
  3. SQL connection closure problem.
    By cbarmpar in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2008, 08:42
  4. Checking network availability
    By fullmetalcoder in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2008, 19:23
  5. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.