QT中IP读取后如何选择自己需要。求高手指点
这段是网上找的,但是输出看一堆的。
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
//获取所有网络接口的列表
foreach(QNetworkInterface interface,list)
{ //遍历每一个网络接口
qDebug() << "Device: "<<interface.name();
//设备名
qDebug() << "HardwareAddress: "<<interface.hardwareAddress();
//硬件地址MAC
QList<QNetworkAddressEntry> entryList = interface.addressEntries();
//获取IP地址条目列表,每个条目中包含一个IP地址,一个子网掩码和一个广播地址
foreach(QNetworkAddressEntry entry,entryList)
{//遍历每一个IP地址条目
qDebug()<<"IP Address: "<<entry.ip().toString();
//IP地址
qDebug()<<"Netmask: " <<entry.netmask().toString();
//子网掩码
qDebug()<<"Broadcast: "<<entry.broadcast().toString();
//广播地址
输出结果
Device: "lo"
HardwareAddress: "00:00:00:00:00:00"
IP Address: "127.0.0.1"
Netmask: "255.0.0.0"
Broadcast: "127.255.255.255"
IP Address: "0:0:0:0:0:0:0:1"
Netmask: "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
Broadcast: ""
Device: "eth0"
HardwareAddress: "00:1D:09:1A:F3:CB"
IP Address: "192.168.1.231"
Netmask: "255.255.255.0"
Broadcast: "192.168.1.255"
IP Address: "FE80:0:0:0:21D:9FF:FE1A:F3CB"
Netmask: "FFFF:FFFF:FFFF:FFFF:0:0:0:0"
Broadcast: ""
Device: "pan0"
HardwareAddress: "46:02:2B:E1:47:54"
需要输出显示这些就行了:
Device: "eth0"
HardwareAddress: "00:1D:09:1A:F3:CB"
IP Address: "192.168.1.231"
Netmask: "255.255.255.0"
Broadcast: "192.168.1.255"
HardwareAddress: "46:02:2B:E1:47:54"
[解决办法]
那是遍历了所有的网络设备。如果你只要输出eth0。
你在
qDebug() << "Device: "<<interface.name();
前面加上 interface.name() == "eth0" 的判断语句,决定要不要继续输出。