Get local IP address in Qt

2019-01-14 15:21发布

问题:

Is there a cross-platform way to get the local IP address (i.e. something that looks like 192.168.1.49) of the computer using Qt?

I want to make an FTP server for a Symbian phone and I want to show the IP address the FTP client should connect to.

回答1:

Use QNetworkInterface::allAddresses()

foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
    if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
         qDebug() << address.toString();
}


回答2:

QNetworkInterface::allAddresses() will give you the network addresses. You can then filter the results to IPv4 addresses that are not loopback addresses:

QList<QHostAddress> list = QNetworkInterface::allAddresses();

 for(int nIter=0; nIter<list.count(); nIter++)

  {
      if(!list[nIter].isLoopback())
          if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol )
        qDebug() << list[nIter].toString();

  }


回答3:

If you require more information than just IP addresses (like the subnet), you have to iterate over all the interfaces.

QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
QNetworkInterface eth;

foreach(eth, allInterfaces) {
    QList<QNetworkAddressEntry> allEntries = eth.addressEntries();
    QNetworkAddressEntry entry;
    foreach (entry, allEntries) {
        qDebug() << entry.ip().toString() << "/" << entry.netmask().toString();
    }
}


回答4:

QNetworkInterface returns lots of addresses. you must filter them, to get desirable result:

foreach (const QNetworkInterface &netInterface, QNetworkInterface::allInterfaces()) {
    QNetworkInterface::InterfaceFlags flags = netInterface.flags();
    if( (bool)(flags & QNetworkInterface::IsRunning) && !(bool)(flags & QNetworkInterface::IsLoopBack)){
        foreach (const QNetworkAddressEntry &address, netInterface.addressEntries()) {
            if(address.ip().protocol() == QAbstractSocket::IPv4Protocol)
                qDebug() << address.ip().toString();
        }
    }
}


回答5:

I wanted to get eth1 IP address of my target machine. Answers provided above helped me to get what I wanted: This is how I wrote my function to get me the IP address of the network interface name eth1.

QNetworkInterface eth1Ip = QNetworkInterface::interfaceFromName("eth1");
QList<QNetworkAddressEntry> entries = eth1Ip.addressEntries();
if (!entries.isEmpty()) {
    QNetworkAddressEntry entry = entries.first();
    qDebug() << entry.ip();
}


回答6:

Here is the code I implemented to get: name, IP, netmask and mac address of localhost.

   QString localhostname =  QHostInfo::localHostName();
   QString localhostIP;
   QList<QHostAddress> hostList = QHostInfo::fromName(localhostname).addresses();
   foreach (const QHostAddress& address, hostList) {
       if (address.protocol() == QAbstractSocket::IPv4Protocol && address.isLoopback() == false) {
            localhostIP = address.toString();
       }
   }
   QString localMacAddress;
   QString localNetmask;
   foreach (const QNetworkInterface& networkInterface, QNetworkInterface::allInterfaces()) {
       foreach (const QNetworkAddressEntry& entry, networkInterface.addressEntries()) {
           if (entry.ip().toString() == localhostIP) {
               localMacAddress = networkInterface.hardwareAddress();
               localNetmask = entry.netmask().toString();
               break;
           }
       }
   }
   qDebug() << "Localhost name: " << localhostname;
   qDebug() << "IP = " << localhostIP;
   qDebug() << "MAC = " << localMacAddress;
   qDebug() << "Netmask = " << localNetmask;


标签: c++ qt ip symbian