10.1 获取本机网络信息
10.1 获取本机网络信息
在实际的网络编程中,经常会需要涉及到一些网络通信,但是在进行网络通信之前往往需要先获得本机的网卡上的相关信息,比如网卡的IP地址(包括IPV4或IPV6),子关掩码,MAC地址等。那么Qt中的网络模块network模块就提供了很多的类来获得这些信息,比如可以使用QHostInfo、QNetworkInterface、QNetworkAddressEntry来获得本机的网络信息。
案例:通过Qt所提供的QNetworkInterface类来获取本机的所有的网卡的信息以及网卡中的IP地址,子网掩码和广播地址;并通过QHostInfo获取本机的名称及IP地址。
(1)新建Qt的GUI应用程序,项目名为NetworkInformation,类名也为项目名,基类继承自QWidget类,不需要创建ui界面。
(2)由于使用到了网络模块,因此需要再.pro文件的QT+=后面添加network模块。
(3)在networkinformation.h中添加以下成员,和布局器,以完成基本的界面布局
#ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H
#include <QWidget>
#include <QDebug>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QGridLayout>
#include <QPushButton>
#include <QNetworkInterface>
#include <QHostInfo>
#include <QHostAddress>
class NetworkInformation : public QWidget
{
Q_OBJECT
public:
NetworkInformation(QWidget *parent = nullptr);
~NetworkInformation();
private:
void setInformation();
private:
void slotDeepInfo();
private:
QLabel*m_hostNameLabel;
QLabel*m_ipAddressLabel;
QLineEdit*m_hostNameLineEdit;
QLineEdit*m_ipAddressLineEdit;
QPushButton*m_deepInfoPushButton;
private:
QGridLayout*m_mainLayout;
};
#endif // NETWORKINFORMATION_H
(4)networkinformation.cpp中进行实现
#include "networkinformation.h"
NetworkInformation::NetworkInformation(QWidget *parent):QWidget(parent)
{
m_mainLayout = new QGridLayout(this);
m_hostNameLabel = new QLabel("主机名:");
m_hostNameLineEdit = new QLineEdit;
m_hostNameLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_hostNameLabel,0,0);
m_mainLayout->addWidget(m_hostNameLineEdit,0,1);
m_ipAddressLabel = new QLabel("IP地址:");
m_ipAddressLineEdit = new QLineEdit;
m_hostNameLineEdit->setReadOnly(true);
m_mainLayout->addWidget(m_ipAddressLabel,1,0);
m_mainLayout->addWidget(m_ipAddressLineEdit,1,1);
m_deepInfoPushButton = new QPushButton("详细");
m_mainLayout->addWidget(m_deepInfoPushButton,2,0,1,3);
setInformation();
connect(this->m_deepInfoPushButton,&QPushButton::clicked,this,&NetworkInformation::slotDeepInfo);
}
NetworkInformation::~NetworkInformation()
{
delete m_hostNameLabel;
m_hostNameLabel = nullptr;
delete m_hostNameLineEdit;
m_hostNameLineEdit = nullptr;
delete m_ipAddressLabel;
m_ipAddressLabel = nullptr;
delete m_ipAddressLineEdit;
m_ipAddressLineEdit = nullptr;
delete m_deepInfoPushButton;
m_deepInfoPushButton = nullptr;
delete m_mainLayout;
m_mainLayout = nullptr;
}
void NetworkInformation::setInformation()
{
QHostInfo info;
QString hostName = info.localHostName();
info = info.fromName(info.localHostName());
m_hostNameLineEdit->setText(hostName);
if(info.addresses().isEmpty())
{
QMessageBox::warning(this,QString("提示"),QString("未获取到IP地址!"));
}
else
{
m_ipAddressLineEdit->setText(info.addresses().first().toString());
}
}
void NetworkInformation::slotDeepInfo()
{
QString info;
const QList<QNetworkInterface>&interface = QNetworkInterface::allInterfaces();//我们这里直接获取整个设备的所有的网络设备
for(QList<QNetworkInterface>::const_iterator it = interface.begin();it != interface.end();it++)//it就代表一块物理网卡
{
QString hardwareName = it->name();//设备名称
QString hardwareAddress = it->hardwareAddress();//硬件地址
QString hardware = "设备名称:" + hardwareName + '\n' + "硬件地址:" + hardwareAddress + '\n';
const QList<QNetworkAddressEntry>&addressEntry = it->addressEntries();//硬件的地址信息(一块网卡可以有多个地址)
QString addressInfo;
for(QList<QNetworkAddressEntry>::const_iterator _it = addressEntry.begin();_it != addressEntry.end();_it++)//_it则表示这块物理网卡上所保存的地址信息
{
QString ipAddress = _it->ip().toString();
QString netMask = _it->netmask().toString();
QString broadcast = _it->broadcast().toString();
addressInfo = addressInfo + "IP地址:" + ipAddress + '\n' + "子网掩码:" + netMask + '\n' + "广播地址:" + broadcast + '\n';
}
addressInfo += "\n";
QString signelHardwareInfo = hardware + addressInfo;
info += signelHardwareInfo;
}
QMessageBox::information(this,QString("本机网卡信息"),info);
}
(5)运行程序

(5)对QHostAddress进行一个总结
#include <QCoreApplication>
#include <QHostAddress>
#include <QHostInfo>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication theApp(argc, argv);
/*
* QHostInfo提供了一些用于查找主机IP地址以及获取主机名称的一些方法,另外它还提供了支持同步或异步的方式来查找主机名
* 如果要使用异步的方法查找,就可以使用含有lookup的前缀的相关函数
*/
//1.QHostInfo的几个构造器
{
//QHostInfo(const QHostInfo &other)//拷贝构造
//QHostInfo(int id = -1)//通过查找的ID来构造
}
//2.重载的运算符函数
{
//QHostInfo &operator=(QHostInfo &&other)//从另一个QHostInfo的非const的引用来构造
//QHostInfo &operator=(const QHostInfo &other)//从另一个QHostInfo的常引用来构造
}
//3.成员方法
{
/*
QList<QHostAddress> addresses() const //获取主机名对应的IP地址(这个并不是描述的本地的主机名,因此可有可能这个函数返回的结果是空的链表)
QHostInfo::HostInfoError error() const //如果在查找主机名时失败,则会获得一个对应的错误枚举值,我们可以根据这个错误的枚举值来判断是哪里出了问题
QString errorString() const //以字符串的方式返回获取主机名的过程中的错误
QString hostName() const //返回IP地址所对应的主机的名称
int lookupId() const //返回查找的ID,这个ID可用于构造另一个QHostInfo对象
QList<QHostAddress> QHostInfo::addresses() const//获取主机名所对应的IP地址
void setAddresses(const QList<QHostAddress> &addresses)//在QHostInfo设置很多的主机地址
void setError(QHostInfo::HostInfoError error)//设置错误码
void setErrorString(const QString &str)//设置错误信息字符串
void setHostName(const QString &hostName)//设置主机名称
void setLookupId(int id)//设置查找ID
void swap(QHostInfo &other)//交换两个QHostInfo对象
*/
}
//4.静态成员方法
{
/*
void abortHostLookup(int id)//终止查询指定ID的主机
QHostInfo fromName(const QString &name)//从主机名称来构造一个QHostInfo对象
QString localDomainName()//返回本地计算机的DNS域
QString localHostName()//返回本地计算机的主机名称(这个方法最常用)
int lookupHost(const QString &name, QObject *receiver, const char *member)//异步查找主机
int lookupHost(const QString &name, Functor functor)//异步查找主机,这个是为多线程使用的
int lookupHost(const QString &name, const QObject *context, Functor functor)//异步查找主机
*/
}
//用例(1):获取本地的IP地址信息
{
qDebug() << "----获取本机Host信息----";
QHostInfo info;
qDebug() << "localHostName:" << info.localHostName();
qDebug() << "localDomainName:" << info.localDomainName();
info = info.fromName(info.localHostName());
qDebug() << info.addresses();
qDebug() << info.errorString();
qDebug() << "----END获取本机Host信息----";
}
//用例(2):获取非本地的IP地址信息
{
QHostInfo info;
info = info.fromName("www.baidu.com");//比如我要获取百度的IP地址,那么我就可以通过fromName中写上百度的网址来进行查询
qDebug() << info.addresses();
}
return theApp.exec();
}
(6)通过QHostInfo编写的地址查询工具


本节所有代码:https://files.cnblogs.com/files/blogs/792763/HostInfo.zip?t=1720377920&download=true

浙公网安备 33010602011771号