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
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default check IPAddress existence

    Hi,

    Can anyone tell me how can i retrieve information, whether a particular ipaddress is present or not within the subnet my pc is connected. using QTcpSocket i can use waitforconnection(). But i need quick response. Is there any Qt api to find.???

    Thank you in advance

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

    Default Re: check IPAddress existence

    take a look at QHostInfo.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: check IPAddress existence

    Thanks you.
    Yeah I already checked the same... But this will be used for mapping Name-IP pair right. how can i use this whether host is present or not

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

    Default Re: check IPAddress existence

    by looking at QHostInfo::error, it should return QHostInfo::HostNotFound or you can use ping + QProcess.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: check IPAddress existence

    Hi I tried this also. But i always get host is not found error. can you please give me a sample code to accomplish this. IP Address is 192.168.99.99

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

    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].

  7. 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)

  8. #7
    Join Date
    Feb 2008
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    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.

  9. #8
    Join Date
    May 2013
    Posts
    1
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows
    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 

  10. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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.