PDA

View Full Version : check IPAddress existence



navi1084
2nd September 2009, 08:20
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

spirit
2nd September 2009, 08:24
take a look at QHostInfo.

navi1084
2nd September 2009, 09:00
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

spirit
2nd September 2009, 09:03
by looking at QHostInfo::error, it should return QHostInfo::HostNotFound or you can use ping + QProcess.

navi1084
2nd September 2009, 09:37
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

spirit
2nd September 2009, 10:13
try something like this


...
QProcess *ping = new QProcess(this);
connect(ping, SIGNAL(readyReadStandardOutput()), SLOT(readResult()));
ping->start(QString("ping.exe %1").arg("192.168.99.99"));
...
void Test::readResult()
{
QProcess *ping = qobject_cast<QProcess *>(sender());
if (!ping)
return;
QString res = ping->readAllStandardOutput();
if (!res.contains('%'))
return;
const int percent = res.mid(res.indexOf('('), res.indexOf(')')).section('%', 0, 0).remove('(').toInt();
if (percent == 100) {
qDebug() << "host not found.";
} else {
qDebug() << "host found.";
}
}

PS. it's for Windows.

bmanam
7th March 2012, 09:09
For folks running into raw socket creation permission issues, I'd recommend using fping (http://www.kwakkelflap.com/fping.html) which has many useful switches and utilizes the ICMP.dll to get around the permission problem.

soukupmi
16th May 2013, 17:37
spirit's code expanded (did not catch the TTL-expired case):

...
QProcess *ping = new QProcess(this);
connect(ping, SIGNAL(readyReadStandardOutput()), SLOT(readResult()));
ping->start(QString("ping.exe %1").arg("192.168.99.99"));
...
void Test::readResult()
{
QProcess *ping = qobject_cast<QProcess *>(sender());
if (!ping)
return;
QString res = ping->readAllStandardOutput();
if (!res.contains('%'))
return;
const int percentLost = res.section('(', -1).section('%', 0, 0).toInt();
if (percentLost == 100) {
qDebug() << "host not found.";
} else {
// above is basically the same code until here:
if ( res.contains(QRegExp("=\\d+ms")) ) {
qDebug() << "host found." << res; // actual response time from IP
} else {
qDebug() << "host not found." << res; // TTL expired in transit
}
}
}

ChrisW67
16th May 2013, 21:26
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 (http://en.wikipedia.org/wiki/Address_Resolution_Protocol) 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.