In my application I want to get the hostname & MAC address from an IP address (in my LAN). I used this code to find the hostname, but nothing appeared in lineedit.

QHostInfo HI;
QHostAddress HA("192.168.1.1");
QList<QHostAddress> List;
List.append(HA);
HI.setAddresses(List);
ui->ledHostname->setText(HI.hostName());
有帮助吗?

解决方案 2

I used this code:

QHostInfo HI = QHostInfo::fromName("192.168.1.50");
ui->ledHostname->setText(HI.hostName());

Worked for some ip addresses & shows the host name! For other ip addresses shows the ip address again.

For my MAC problem, I`m using ARP packet.

其他提示

To retrieve the Hostname from an IP address you can call lookupHost(), which takes the host name or IP address, a receiver object, and a slot signature as arguments. The slot is invoked when the results are ready. The results are stored in a QHostInfo object. Call addresses() to get the list of IP addresses for the host, and hostName() to get the host name that was looked up.

QHostInfo::lookupHost("92.168.1.1",
                   this, SLOT(lookedUp(QHostInfo)));

void MyWidget::lookedUp(const QHostInfo &host)
 {
     if (host.error() != QHostInfo::NoError) {
         qDebug() << "Lookup failed:" << host.errorString();
         return;
     }

     foreach (const QHostAddress &address, host.addresses())
         qDebug() << "Found address:" << address.toString();
 }

For obtaining the MAC address of a remote IP you should use system commands and platform-specific code. There is no way in Qt to do that. For example on Windows it can be done by:

arp -a <IP>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top