Get local IP address in Qt

前端 未结 6 1116
借酒劲吻你
借酒劲吻你 2020-12-05 17:26

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

相关标签:
6条回答
  • 2020-12-05 17:54

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 18:05

    Use QNetworkInterface::allAddresses()

    const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
    for (const QHostAddress &address: QNetworkInterface::allAddresses()) {
        if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost)
             qDebug() << address.toString();
    }
    
    0 讨论(0)
  • 2020-12-05 18:05

    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();
    
      }
    
    0 讨论(0)
  • 2020-12-05 18:05

    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;
    
    0 讨论(0)
  • 2020-12-05 18:07

    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();
    }
    
    0 讨论(0)
  • 2020-12-05 18:15

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题