Results 1 to 9 of 9

Thread: check IPAddress existence

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: check IPAddress existence

    try something like this
    Qt Code:
    1. ...
    2. QProcess *ping = new QProcess(this);
    3. connect(ping, SIGNAL(readyReadStandardOutput()), SLOT(readResult()));
    4. ping->start(QString("ping.exe %1").arg("192.168.99.99"));
    5. ...
    6. void Test::readResult()
    7. {
    8. QProcess *ping = qobject_cast<QProcess *>(sender());
    9. if (!ping)
    10. return;
    11. QString res = ping->readAllStandardOutput();
    12. if (!res.contains('%'))
    13. return;
    14. const int percent = res.mid(res.indexOf('('), res.indexOf(')')).section('%', 0, 0).remove('(').toInt();
    15. if (percent == 100) {
    16. qDebug() << "host not found.";
    17. } else {
    18. qDebug() << "host found.";
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    PS. it's for Windows.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  2. The following 3 users say thank you to spirit for this useful post:

    bmanam (7th March 2012), navi1084 (2nd September 2009), soukupmi (16th May 2013)

  3. #2
    Join Date
    Feb 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: check IPAddress existence

    For folks running into raw socket creation permission issues, I'd recommend using fping which has many useful switches and utilizes the ICMP.dll to get around the permission problem.

  4. #3
    Join Date
    May 2013
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows
    Thanks
    1
    Wiki edits
    4

    Default Re: check IPAddress existence

    spirit's code expanded (did not catch the TTL-expired case):
    Qt Code:
    1. ...
    2. QProcess *ping = new QProcess(this);
    3. connect(ping, SIGNAL(readyReadStandardOutput()), SLOT(readResult()));
    4. ping->start(QString("ping.exe %1").arg("192.168.99.99"));
    5. ...
    6. void Test::readResult()
    7. {
    8. QProcess *ping = qobject_cast<QProcess *>(sender());
    9. if (!ping)
    10. return;
    11. QString res = ping->readAllStandardOutput();
    12. if (!res.contains('%'))
    13. return;
    14. const int percentLost = res.section('(', -1).section('%', 0, 0).toInt();
    15. if (percentLost == 100) {
    16. qDebug() << "host not found.";
    17. } else {
    18. // above is basically the same code until here:
    19. if ( res.contains(QRegExp("=\\d+ms")) ) {
    20. qDebug() << "host found." << res; // actual response time from IP
    21. } else {
    22. qDebug() << "host not found." << res; // TTL expired in transit
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: check IPAddress existence

    There is no guaranteed way to determine if a particular IP address is in use from a high level program. On a LAN you might be able to use ping or attempt to connect to a well known service that these machines might offer but many machines firewall these by default now.

    You can use ARP to find if a machine claims the address, or query the system ARP table after attempting to connect another way. There is no Qt mechanism for doing this.

Similar Threads

  1. Replies: 0
    Last Post: 2nd May 2008, 07:57
  2. Check a point inside or outside a road?
    By kstking in forum Qt Programming
    Replies: 1
    Last Post: 15th October 2007, 18:48
  3. Check and Uncheck on a Dir Tree?
    By vishal.chauhan in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2007, 11:55
  4. How to check a file for changes since last save
    By nmather in forum General Programming
    Replies: 2
    Last Post: 21st April 2007, 23:43
  5. Directpry
    By shailesh in forum Qt Programming
    Replies: 10
    Last Post: 11th April 2006, 10:08

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
  •  
Qt is a trademark of The Qt Company.