美文网首页QT
[Qt]Cpp 获取本机的IP地址

[Qt]Cpp 获取本机的IP地址

作者: _小老虎_ | 来源:发表于2020-02-01 00:55 被阅读0次

    QString changyanlong_xiaolaohu::IpAddress() {
        QString strIpAddress;
        QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    
        // find ipv4 address
        int nListSize = ipAddressesList.size();
        for (int i = 0; i < nListSize; ++i)
        {
               if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
                   ipAddressesList.at(i).toIPv4Address()) {
                   strIpAddress = ipAddressesList.at(i).toString();
                   break;
               }
         }
    
         // if v4 is none , find local ip address
         if (strIpAddress.isEmpty())
            strIpAddress = QHostAddress(QHostAddress::LocalHost).toString();
         return strIpAddress;
    }
    

    相关文章

      网友评论

        本文标题:[Qt]Cpp 获取本机的IP地址

        本文链接:https://www.haomeiwen.com/subject/maktxhtx.html