C++ 在 Qt 中获取本地 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13835989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:45:10  来源:igfitidea点击:

Get local IP address in Qt

c++qtipsymbian

提问by sashoalm

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?

有没有一种跨平台的方法来192.168.1.49使用 Qt 获取计算机的本地 IP 地址(即看起来像的东西)?

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.

我想为 Symbian 手机建立一个 FTP 服务器,并且我想显示 FTP 客户端应该连接到的 IP 地址。

回答by benjarobin

Use QNetworkInterface::allAddresses()

使用QNetworkInterface::allAddresses()

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

回答by Prasoon

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

QNetworkInterface::allAddresses()会给你网络地址。然后,您可以将结果过滤到不是环回地址的 IPv4 地址:

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();

  }

回答by Dan

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

如果您需要的信息不仅仅是 IP 地址(如子网),您必须遍历所有接口。

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();
    }
}

回答by Hamed Nikzad

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

QNetworkInterface 返回大量地址。你必须过滤它们,以获得理想的结果:

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();
        }
    }
}

回答by user2434119

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

这是我实现的代码:localhost 的名称、IP、网络掩码和 mac 地址。

   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;

回答by user12345

I wanted to get eth1IP 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.

我想获取eth1目标机器的 IP 地址。上面提供的答案帮助我得到了我想要的:这就是我编写函数以获取网络接口名称的 IP 地址的方式eth1

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