当前位置:首页 » 《随便一记》 » 正文

qt准确获取本机mac和ip地址

23 人参与  2022年12月16日 09:29  分类 : 《随便一记》  评论

点击全文阅读


前言

公司服务器有多个mac地址,是用网上的代码获取时总是不准确,只能自己封装一下。qt通过QNetworkInterface类来获取网络接口的各种信息,我是通过解析QNetworkInterface数组来获取ip和mac的


先上代码,不想听原理的同学可以直接复制代码

准确获取mac地址

QString MainWindow::getHostMacAddress(){        QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表        int nCnt = nets.count();        qDebug()<<nets;        QString strMacAddr = "";        for(int i = 0; i < nCnt; i ++)        {            // 如果此网络接口被激活并且正在运行并且不是回环地址,且该mac的ip地址不能是回环地址并且是ipv4地址,则就是我们需要找的Mac地址            if(nets[i].flags().testFlag(QNetworkInterface::IsUp) &&                nets[i].flags().testFlag(QNetworkInterface::IsRunning)               && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))            {                for (int j=0;j<nets[i].addressEntries().size() ;j++ ) {                //该mac的ip地址不能为172.0.0.1,且需是ipv4地址                    if(nets[i].addressEntries().at(j).ip()!=QHostAddress::LocalHost&&nets[i].addressEntries().at(j).ip().protocol()== QAbstractSocket::IPv4Protocol){                        strMacAddr = nets[i].hardwareAddress();                    }                }            }        }        qDebug()<<"strMacAddr"<<strMacAddr;        return strMacAddr;}

准确获取ip地址

QString MainWindow::getHostLocalIpAddress(){    QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表    int nCnt = nets.count();    QString strIpAddr = "";    for(int i = 0; i < nCnt; i ++)    {        // 如果此网络接口被激活并且正在运行并且不是回环地址,且该mac的ip地址不能是回环地址并且是ipv4地址,则就是我们需要找的ip地址        if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning)                && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))        {            for (int j=0;j<nets[i].addressEntries().size() ;j++ ) {                if(nets[i].addressEntries().at(j).ip()!=QHostAddress::LocalHost&&nets[i].addressEntries().at(j).ip().protocol()== QAbstractSocket::IPv4Protocol){                    strIpAddr=nets[i].addressEntries().at(j).ip().toString();                }            }        }    }    qDebug()<<"strIpAddr"<<strIpAddr;    return strIpAddr;}

原理分析

首先解析一下QList nets = QNetworkInterface::allInterfaces();网络接口信息

(QNetworkInterface(name = “lo”, hardware address = “00:00:00:00:00:00”, flags = IsUp IsRunning IsLoopBack , entries = ((address = QHostAddress(“127.0.0.1”), netmask = QHostAddress(“255.0.0.0”), broadcast = QHostAddress(“127.255.255.255”)), (address = QHostAddress(“::1”), netmask = QHostAddress(“ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff”))))
, QNetworkInterface(name = “enp2s0”, hardware address = “74:27:EA:27:8A:71”, flags = IsUp IsRunning CanBroadcast CanMulticast , entries = ((address = QHostAddress(“172.16.52.51”), netmask = QHostAddress(“255.255.255.0”), broadcast = QHostAddress(“172.16.52.255”)), (address = QHostAddress(“fe80::7627:eaff:fe27:8a71%enp2s0”), netmask = QHostAddress(“ffff:ffff:ffff:ffff::”))))
, QNetworkInterface(name = “virbr0”, hardware address = “52:54:00:A7:FB:D6”, flags = CanBroadcast CanMulticast , entries = ((address = QHostAddress(“192.168.122.1”), netmask = QHostAddress(“255.255.255.0”), broadcast = QHostAddress(“192.168.122.255”))))
)

或许你可能觉得看起来很复杂,让我们提取enp2s0中的信息

QNetworkInterface(name = "enp2s0", hardware address = "74:27:EA:27:8A:71",flags = IsUp IsRunning CanBroadcast CanMulticast , entries = ((address = QHostAddress("172.16.52.51"), netmask = QHostAddress("255.255.255.0"),broadcast = QHostAddress("172.16.52.255")),(address = QHostAddress("fe80::7627:eaff:fe27:8a71%enp2s0"), netmask = QHostAddress("ffff:ffff:ffff:ffff::"))))

这样看起来就一目了然了,正确的mac和ip地址必须要满足:flags = IsUp IsRunning,且falgs不能有IsLoopBack。address = QHostAddress(“172.16.52.51”)中的ip地址不能是172.0.0.1且必须是ipv4地址


想要详细了解QNetworkInterface类的同学可自行查看qt说明文档,或下面这篇文章

链接:QNetworkInterface详解


码字不易,如果这篇博客对你有帮助,麻烦点赞收藏,非常感谢!有不对的地方


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/49851.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1