Qt5获取网卡/IP等信息
参考网址:http://blog.csdn.net/wjs1033/article/details/22697063
1、环境 Win7x64、Qt5.5.1(x86)、vs2013_ultimate_up5(x86)
1.1、?.h / ?.cpp 都保存成“UTF-8 + BOM”格式(这样,源码里面中文/特殊符号,使用中文注释,就不会有 error或warnning了)
1.2、?.h / ?.cpp 都保存成“UTF-8 + BOM”格式 的话,qDebug()输出中文的时候 全是乱码...
?.h / ?.cpp 都保存成“UTF-8” 格式 的话,qDebug()输出中文的时候 小部分是乱码...
ZC: 暂时 先将 ?.h / ?.cpp 都保存成“UTF-8”格式,以后再研究 这些字符串编码的 中文乱码的事情吧...
2、代码:
2.1、?.pro
#-------------------------------------------------
#
# Project created by QtCreator 2016-11-14T16:10:00
#
#-------------------------------------------------
QT += core gui \
network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = NetworkInterfaceZ
TEMPLATE = app
SOURCES += main.cpp\
MainWindow.cpp
HEADERS += MainWindow.h
FORMS += MainWindow.ui
2.2、MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QHostInfo> // http://blog.csdn.net/wjs1033/article/details/22697063 namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pbtnMsg01_clicked(); void on_pbtnMsg02_clicked(); void on_pbtnMsg03_clicked(); void on_pbtnMsg04_clicked(); void on_pbtnMsg05_clicked(); private: Ui::MainWindow *ui; public slots: void LookupHostZ(const QHostInfo &_hostInfo); }; #endif // MAINWINDOW_H
2.3、MainWindow.cpp
#include "MainWindow.h" #include "ui_MainWindow.h" #include <QNetworkInterface> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // 枚举本机的网络连接并获取其属性 void MainWindow::on_pbtnMsg01_clicked() { int i=0, j=0; QList<QNetworkInterface> networkInterface = QNetworkInterface::allInterfaces(); for (QList<QNetworkInterface>::const_iterator it = networkInterface.constBegin(); it != networkInterface.constEnd(); ++it) { qDebug() << "[" << i << "] : " << (*it).name(); qDebug() << " " << (*it).humanReadableName(); qDebug() << " " << (*it).hardwareAddress(); //获取连接地址列表 QList<QNetworkAddressEntry> addressEntriesList = (*it).addressEntries(); for (QList<QNetworkAddressEntry>::const_iterator jIt = addressEntriesList.constBegin(); jIt != addressEntriesList.constEnd(); ++jIt) { qDebug() << "\t(" << j << ") :"; //输出 ip qDebug() << "\t\tIP : " <<(*jIt).ip().toString(); //输出 netmask qDebug() << "\t\tnetmask(子网掩码) : " << (*jIt).netmask().toString(); qDebug() << "\t\tBroadcast(广播地址) : "<< (*jIt).broadcast().toString(); qDebug() << ""; j ++; } i ++; } } // 只枚举ip地址的简洁方式 void MainWindow::on_pbtnMsg02_clicked() { QList<QHostAddress> list = QNetworkInterface::allAddresses(); { foreach(QHostAddress address,list) { if(address.protocol() == QAbstractSocket::IPv4Protocol) qDebug() << address.toString(); } } } // 获取本机主机名 及 IPv4地址 void MainWindow::on_pbtnMsg03_clicked() { QString strLocalHostName = QHostInfo::localHostName(); // 获取主机名 qDebug() << "本地主机名 : " << strLocalHostName; QHostInfo info = QHostInfo::fromName(strLocalHostName);// 根据上边获得的主机名来获取本机的信息 // QHostInfo的address函数获取本机ip地址 // QHostAddress类是管理ip地址的类,所有的ip都归这个类管理。 foreach (QHostAddress address, info.addresses()) { if(address.protocol() == QAbstractSocket::IPv4Protocol)//只取ipv4协议的地址 qDebug() << "IP(IPv4Protocol) : " << address.toString(); } } // 通过 主机名 获取IP地址 void MainWindow::on_pbtnMsg04_clicked() { // 以主机名获取ip (会调用slot函数LookupHostZ) QHostInfo::lookupHost("www.baidu.com", this, SLOT(LookupHostZ(QHostInfo))); } // 通过IP地址 反向查询 主机名 void MainWindow::on_pbtnMsg05_clicked() { // 通过ip地址获取主机名 (会调用slot函数LookupHostZ) QHostInfo::lookupHost("192.168.1.201", this, SLOT(LookupHostZ(QHostInfo))); } // 响应QHostInfo::lookupHost(...) 的slot函数 void MainWindow::LookupHostZ(const QHostInfo &_hostInfo) { // 输出IPv4的IP地址 foreach (QHostAddress address, _hostInfo.addresses()) { if(address.protocol() == QAbstractSocket::IPv4Protocol)//只取ipv4协议的地址 qDebug() << "LookupHostZ - IP(IPv4Protocol) : " << address.toString(); } // 输出主机名 qDebug() << _hostInfo.hostName(); }
2.4、MainWindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>518</width> <height>354</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <widget class="QPushButton" name="pbtnMsg01"> <property name="geometry"> <rect> <x>20</x> <y>10</y> <width>211</width> <height>23</height> </rect> </property> <property name="text"> <string>枚举本机的网络连接并获取其属性</string> </property> </widget> <widget class="QPushButton" name="pbtnMsg02"> <property name="geometry"> <rect> <x>20</x> <y>40</y> <width>211</width> <height>23</height> </rect> </property> <property name="text"> <string>只枚举ip地址的简洁方式</string> </property> </widget> <widget class="QPushButton" name="pbtnMsg03"> <property name="geometry"> <rect> <x>20</x> <y>90</y> <width>211</width> <height>23</height> </rect> </property> <property name="text"> <string>获取本机主机名及IPv4地址</string> </property> </widget> <widget class="QPushButton" name="pbtnMsg04"> <property name="geometry"> <rect> <x>20</x> <y>140</y> <width>211</width> <height>23</height> </rect> </property> <property name="text"> <string>通过主机名获取IP地址</string> </property> </widget> <widget class="QPushButton" name="pbtnMsg05"> <property name="geometry"> <rect> <x>20</x> <y>170</y> <width>211</width> <height>23</height> </rect> </property> <property name="text"> <string>通过IP地址反向查询主机名</string> </property> </widget> </widget> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
2.5、界面截图
3、程序运行输出:
3.1、枚举本机的网络连接并获取其属性
[ 0 ] : "{7558E85A-8386-4A5F-A5BF-4A4F22792AA4}"
"无线网络连接"
"34:23:87:61:DF:31"
( 0 ) :
IP : "fe80::a4d3:7dfd:62cf:558d%14"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : ""
( 1 ) :
IP : "169.254.85.141"
netmask(子网掩码) : ""
Broadcast(广播地址) : ""
[ 1 ] : "{3B25081D-362E-42E8-8FDF-B6D5A99824CF}"
"本地连接"
"28:D2:44:40:5B:C1"
( 2 ) :
IP : "fe80::e58e:6f0e:df37:e8ed%12"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : ""
( 3 ) :
IP : "192.168.1.233"
netmask(子网掩码) : "255.255.255.0"
Broadcast(广播地址) : "192.168.1.255"
[ 2 ] : "{B5D6033F-8002-4428-80A3-1974993AF12A}"
"Bluetooth 网络连接"
"34:23:87:61:DF:32"
( 4 ) :
IP : "fe80::e0b7:f552:784e:9fe%11"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : ""
( 5 ) :
IP : "169.254.9.254"
netmask(子网掩码) : ""
Broadcast(广播地址) : ""
[ 3 ] : "{8376C30F-73E1-4029-B465-AB5449D3812E}"
"VMware Network Adapter VMnet1"
"00:50:56:C0:00:01"
( 6 ) :
IP : "fe80::29c7:6a:97d7:bb0f%19"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : ""
( 7 ) :
IP : "192.168.131.1"
netmask(子网掩码) : "255.255.255.0"
Broadcast(广播地址) : "192.168.131.255"
[ 4 ] : "{1D0B26ED-F9CB-43A4-AD64-202A6D727BFA}"
"VMware Network Adapter VMnet8"
"00:50:56:C0:00:08"
( 8 ) :
IP : "fe80::a567:53d5:a0b5:cd63%20"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : ""
( 9 ) :
IP : "192.168.181.1"
netmask(子网掩码) : "255.255.255.0"
Broadcast(广播地址) : "192.168.181.255"
[ 5 ] : "{846EE342-7039-11DE-9D20-806E6F6E6963}"
"Loopback Pseudo-Interface 1"
""
( 10 ) :
IP : "::1"
netmask(子网掩码) : "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
Broadcast(广播地址) : ""
( 11 ) :
IP : "127.0.0.1"
netmask(子网掩码) : ""
Broadcast(广播地址) : ""
[ 6 ] : "{DA5C1007-9FC1-4637-AFBF-CFC713356DDA}"
"isatap.{3B25081D-362E-42E8-8FDF-B6D5A99824CF}"
"00:00:00:00:00:00:00:E0"
( 12 ) :
IP : "fe80::5efe:c0a8:1e9%15"
netmask(子网掩码) : "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
Broadcast(广播地址) : ""
[ 7 ] : "{B6A0553B-7196-4747-87B8-D1307AA54F7E}"
"Teredo Tunneling Pseudo-Interface"
"00:00:00:00:00:00:00:E0"
( 13 ) :
IP : "fe80::100:7f:fffe%13"
netmask(子网掩码) : "ffff:ffff:ffff:ffff::"
Broadcast(广播地址) : ""
[ 8 ] : "{B61A7FDB-E0C1-42CC-A324-54F35789A245}"
"isatap.{8376C30F-73E1-4029-B465-AB5449D3812E}"
"00:00:00:00:00:00:00:E0"
( 14 ) :
IP : "fe80::5efe:c0a8:8301%16"
netmask(子网掩码) : "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
Broadcast(广播地址) : ""
[ 9 ] : "{6B68472C-AA56-44A8-A9AE-FFEF1A4ED978}"
"isatap.{7558E85A-8386-4A5F-A5BF-4A4F22792AA4}"
"00:00:00:00:00:00:00:E0"
[ 10 ] : "{BAAD8784-EFFA-4C76-AE9E-8074A9965DBA}"
"isatap.{B5D6033F-8002-4428-80A3-1974993AF12A}"
"00:00:00:00:00:00:00:E0"
[ 11 ] : "{7F3F01D3-A615-4238-8453-632DCB4C13C6}"
"isatap.{1D0B26ED-F9CB-43A4-AD64-202A6D727BFA}"
"00:00:00:00:00:00:00:E0"
( 15 ) :
IP : "fe80::5efe:c0a8:b501%21"
netmask(子网掩码) : "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
Broadcast(广播地址) : ""
3.2、只枚举ip地址的简洁方式
"169.254.85.141"
"192.168.1.233"
"169.254.9.254"
"192.168.131.1"
"192.168.181.1"
"127.0.0.1"
3.3、获取本机主机名 及 IPv4地址 (这里的 控制台输出 中文会有乱码)
"本地主机???: " "33-PC"
IP(IPv4Protocol) : "192.168.1.233"
IP(IPv4Protocol) : "192.168.131.1"
IP(IPv4Protocol) : "192.168.181.1"
3.4、通过 主机名 获取IP地址
LookupHostZ - IP(IPv4Protocol) : "180.97.33.108"
LookupHostZ - IP(IPv4Protocol) : "180.97.33.107"
"www.baidu.com"
3.5、通过IP地址 反向查询 主机名 (这个执行的时候,用时较长)
LookupHostZ - IP(IPv4Protocol) : "192.168.1.201"
"ZHEJIANG001"
3.6、
4、获取本机的 第一个IPv4地址的字符串 (Windows下)
传入参数:_strNetworkInterfaceHumanReadableName : 网络接口卡的名称(类似"无线网络连接"、"本地连接"等)
QString Widget::getIP(QString _strNetworkInterfaceHumanReadableName) { //QString str01 = QString::fromLocal8Bit("本地连接"); QList<QNetworkInterface> networkInterface = QNetworkInterface::allInterfaces(); foreach (QNetworkInterface intf, networkInterface) { bool b = false; if (_strNetworkInterfaceHumanReadableName == NULL) b = true; else { QString str = intf.humanReadableName(); if (str.startsWith(_strNetworkInterfaceHumanReadableName, Qt::CaseInsensitive)) b = true; } if (b) { QList<QNetworkAddressEntry> listNAE = intf.addressEntries(); foreach (QNetworkAddressEntry nae, listNAE) { QHostAddress ha = nae.ip(); if (ha.protocol() == QAbstractSocket::IPv4Protocol) return ha.toString(); } } } return NULL; }
5、