Qt获取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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
准确获取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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
原理分析
首先解析一下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::"))
)
)
1
2
3
4
5
6
7
8
9
这样看起来就一目了然了,正确的mac和ip地址必须要满足:flags = IsUp IsRunning,且falgs不能有IsLoopBack。address = QHostAddress(“172.16.52.51”)中的ip地址不能是172.0.0.1且必须是ipv4地址
想要详细了解QNetworkInterface类的同学可自行查看qt说明文档,或下面这篇文章
链接:QNetworkInterface详解
码字不易,如果这篇博客对你有帮助,麻烦点赞收藏,非常感谢!有不对的地方
文章知识点与官方知识档案匹配,可进一步学习相关知识
网络技能树首页
————————————————
版权声明:本文为CSDN博主「小土同学!」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44092851/article/details/124814913