欢迎小伙伴的点评✨✨,相互学习c/c++应用开发。🍳🍳🍳
博主🧑🧑 本着开源的精神交流Qt开发的经验、将持续更新续章,为社区贡献博主自身的开源精神👩🚀
目录
前言
本章节会给大家带来Qt5 网络与通信—— 获取本机网络信息实例详解。
一、Qt5 网络与通信概述
在应用程序开发中,网络编程非常重要。目前,互联网通行的 TCP/IP 协议自上而下地分为应用层、传输层、网际层和网络接口层这四层。实际编写网络应用程序时只使用传输层和应用层,所涉及的协议主要包括 UDP 、 TCP 、 FTP 和 HTTP 等。
虽然目前主流的操作系统 (Windows 、 Linux 等)都提供了统一的套接字 (Socket) 抽象编程接口 CAPO, 用千编写不同层次的网络程序,但是这种方式比较烦琐,甚至有时需要引用底层操作系统的相关数据结构,而 Qt 提供的网络模块 QtNetwork 圆满地解决了这一问题。
1.1、获取本机网络信息概述
在网络应用中,经常需要获取本机的主机名、 IP 地址和硬件地址等网络信息。运用QHostlnfo 、 QNetworkinterface 、 QNetworkAddressEntry 可获取本机的网络信息。
二、效果实例
图一
图二
网络编程需要加入network在 xxx.pro中具体如图二
三、原码解析
3.1首先先在新建好的工程文件xxx.pro文件中加入
QT += network
networkinformation.h
#ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
#include <QMessageBox>
#include <QHostInfo>
#include <QNetworkInterface>
class Networkinformation : public QWidget
{
Q_OBJECT
public:
Networkinformation(QWidget *parent = 0);
~Networkinformation();
void getHostinformation ();
public slots:
void slotDetail();
private:
QLabel *hostLabel;
QLineEdit *LineEditLocalHostName;
QLabel *ipLabel;
QLineEdit *LineEditAddress;
QPushButton *detailBtn;
QGridLayout *mainLayout;
};
#endif // NETWORKINFORMATION_H
main.cpp
#include "networkinformation.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Networkinformation w;
w.show();
return a.exec();
}
networkinformation.cpp
#include "networkinformation.h"
Networkinformation::Networkinformation(QWidget *parent)
: QWidget(parent)
{
hostLabel = new QLabel(tr("主机名: ")) ;
LineEditLocalHostName= new QLineEdit;
ipLabel = new QLabel(tr("IP 地址: ")) ;
LineEditAddress = new QLineEdit;
detailBtn = new QPushButton(tr("详细")) ;
mainLayout = new QGridLayout(this);
mainLayout->addWidget(hostLabel,0,0);
mainLayout->addWidget(LineEditLocalHostName,0,1);
mainLayout->addWidget(ipLabel,1,0);
mainLayout->addWidget(LineEditAddress,1,1);
mainLayout->addWidget(detailBtn,2,0,1,2);
getHostinformation();
connect (detailBtn, SIGNAL (clicked()), this, SLOT (slotDetail ()));
}
Networkinformation::~Networkinformation()
{
}
void Networkinformation::getHostinformation ()
{
QString localHostName = QHostInfo::localHostName(); /*获取本机主机名。: QHostlnfo 提
供了一系列有关网络信息的静态函数,可以根据主机名获取分配的 IP 地址,也可以根据 IP 地址
获取相应的主机名。*/
LineEditLocalHostName->setText(localHostName);
QHostInfo hostInfo = QHostInfo::fromName (localHostName); /*根据主机名获取相关主机
信息,包括 1P 地址等。 QHostlnfo ::fromName()函数通过主机名查找 IP 地址信息。*/
QList<QHostAddress> listAddress = hostInfo.addresses();
if (! listAddress. isEmpty()) /*获取的主机 IP 地址列表可能为空。在不为空的情况下使
用第一个 IP 地址。*/
{
LineEditAddress->setText(listAddress. at(1).toString ()) ;
}
}
/*slotDetail() 函数获取与网络接口相关的信息*/
void Networkinformation::slotDetail()
{
QString detail="";
QList <QNetworkInterface> list=QNetworkInterface::allInterfaces();/*QNetworkInterface类
提供了一个主机 IP 地址和网络接口的列表。*/
for (int i=0; i<list.count(); i++)
{
QNetworkInterface interface=list.at(i);
detail=detail+tr(" 设备: ")+interface.name() +"\n";
// 获取网络接口的名称。
detail=detail+tr(" 硬件地址: ")+interface.hardwareAddress () +" \n";
// 获取网络接口的硬件地址。
QList<QNetworkAddressEntry> entryList=interface.addressEntries();
/*每个网络接口包括 0 个或多个 IP 地址,每个 IP 地址有选择性
地与一个子网掩码和(或)一个广播地址相关联。 QNetworkAddressEntry 类存储了被网络接口支
待的一个 IP 地址,同时还包括与之相关的子网掩码和广播地址。*/
for(int j=1;j<entryList.count(); j++)
{
QNetworkAddressEntry entry=entryList.at(j);
detail=detail+"\t"+tr("IP 地址: ")+entry. ip () . toString () +"\n";
detail=detail+"\t"+tr("子网掩码: ")+entry.netmask().toString() +"\n";
detail=detail+"\t"+tr("广播地址: ")+entry.broadcast().toString () +"\n";
}
}
QMessageBox::information(this,tr("Detail"),detail);
}
四、总结
Qt5 网络与通信—— 获取本机网络信息会在应用程序开发中经常用到的。