????????Qt 網(wǎng)絡(luò)模塊為我們提供了編寫TCP/IP客戶端和服務(wù)器的類。它提供了較低級(jí)別的類,例如代表低級(jí)網(wǎng)絡(luò)概念的 QTcpSocket,QTcpServer 和 QUdpSocket,以及諸如 QNetworkRequest, QNetworkReply 和 QNetworkAccessManager 之類的高級(jí)類來執(zhí)行使用通用協(xié)議的網(wǎng)絡(luò)操作。它 還提供了諸如QNetworkConfiguration,QNetworkConfigurationManager和QNetworkSession等類, 實(shí)現(xiàn)承載管理。
????????想要在程序中使用 Qt 網(wǎng)絡(luò)模塊,我們需要在 pro 項(xiàng)目配置文件里增加下面的一條語句。
QT += network
獲取本機(jī)的網(wǎng)絡(luò)信息
????????為什么先獲取本機(jī)網(wǎng)絡(luò)信息呢?在建立網(wǎng)絡(luò)通信之前我們至少得獲取對(duì)方的 IP 地址。在網(wǎng)絡(luò)應(yīng)用中,經(jīng)常需要用到本機(jī)的主機(jī)名、IP 地址、MAC 地址等網(wǎng)絡(luò)信息,通常通在 Windows 通過調(diào)出命令行 cmd 窗口輸入ipconfig或者在 Linux 系統(tǒng)中使用ifconfig命令就可以查看相關(guān)信息了,在這里我們利用Qt做出一個(gè)可以查詢的界面和功能出來,為了后面的網(wǎng)絡(luò)編程打下一個(gè)簡單的基礎(chǔ)。
????????Qt 提供了 QHostInfo 和 QNetworkInterface 類可以用于此類信息查詢。更多關(guān)于 QHostInfo 和 QNetworkInterface 的相關(guān)函數(shù)可以在 Qt 的幫助文檔中找到。下面我們寫代碼時(shí)會(huì)使用到相關(guān)的函數(shù),有清楚的注釋。
應(yīng)用實(shí)例
? ? ? ? 項(xiàng)目目的:了解如何通過QHostInfo和QNetworkInterface類獲取本地網(wǎng)絡(luò)所有接口的信息。
????????項(xiàng)目名稱:networkhostinfo ,獲取本機(jī)網(wǎng)絡(luò)接口信息。????????
????????獲取本機(jī)的網(wǎng)絡(luò)接口信息,打印在文本瀏覽框上,點(diǎn)擊按鈕可直接獲取,為了清楚看見是重新獲取的過程,本例點(diǎn)擊獲取本機(jī)信息按鈕后延時(shí) 1s 去刷新獲取的信息。點(diǎn)擊另一個(gè)清空文本信息按鈕可以清空文本瀏覽框上的文本內(nèi)容。
????????項(xiàng)目文件 networkhostinfo.pro 文件第一行添加的代碼部分如下。
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
????????在頭文件“mainwindow.h”具體代碼如下。
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QTextBrowser>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTimer>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
/* 點(diǎn)擊獲取和清空文本按鈕 */
QPushButton *pushButton[2];
/* 文本瀏覽框用于顯示本機(jī)的信息 */
QTextBrowser *textBrowser;
/* 水平Widget容器和垂直Widget容器*/
QWidget *hWidget;
QWidget *vWidget;
/* 水平布局和垂直布局 */
QHBoxLayout *hBoxLayout;
QVBoxLayout *vBoxLayout;
/* 定時(shí)器 */
QTimer *timer;
/* 獲取本機(jī)的網(wǎng)絡(luò)的信息,返回類型是QString */
QString getHostInfo();
private slots:
/* 定時(shí)器槽函數(shù),點(diǎn)擊按鈕后定時(shí)觸發(fā) */
void timerTimeOut();
/* 顯示本機(jī)信息 */
void showHostInfo();
/* 啟動(dòng)定時(shí)器 */
void timerStart();
/* 清空textBrowser的信息 */
void clearHostInfo();
};
#endif // MAINWINDOW_H
????????頭文件里主要是聲明兩個(gè)按鈕和一個(gè)文本瀏覽框。另外還有一個(gè)定時(shí)器,聲明一些槽函數(shù), 比較簡單。 在源文件“mainwindow.cpp”具體代碼如下。
#include "mainwindow.h"
#include <QNetworkInterface>
#include <QHostInfo>
#include <QThread>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
/* 設(shè)置位置與大小 */
this->setGeometry(0, 0, 800, 480);
/* 點(diǎn)擊獲取本地信息按鈕和清空文本按鈕 */
pushButton[0] = new QPushButton();
pushButton[1] = new QPushButton();
pushButton[0]->setText("獲取本機(jī)信息");
pushButton[1]->setText("清空文本信息");
/* 按鈕的大小根據(jù)文本自適應(yīng),
* 注意setSizePolicy需要在布局中使用 */
pushButton[0]->setSizePolicy(QSizePolicy::Fixed,
QSizePolicy::Fixed);
pushButton[1]->setSizePolicy(QSizePolicy::Fixed,
QSizePolicy::Fixed);
/* 水平Widget和垂直Widget用于添加布局 */
hWidget = new QWidget();
vWidget = new QWidget();
/* 水平布局和垂直布局 */
hBoxLayout = new QHBoxLayout();
vBoxLayout = new QVBoxLayout();
/* 文本瀏覽框 */
textBrowser = new QTextBrowser();
/* 添加到水平布局 */
hBoxLayout->addWidget(pushButton[0]);
hBoxLayout->addWidget(pushButton[1]);
/* 將水平布局設(shè)置為hWidget的布局 */
hWidget->setLayout(hBoxLayout);
/* 將文本瀏覽框和hWidget添加到垂直布局 */
vBoxLayout->addWidget(textBrowser);
vBoxLayout->addWidget(hWidget);
/* 將垂直布局設(shè)置為vWidget的布局 */
vWidget->setLayout(vBoxLayout);
/* 設(shè)置vWidget為中心部件 */
setCentralWidget(vWidget);
/* 定時(shí)器初始化 */
timer = new QTimer();
/* 信號(hào)槽連接 */
connect(pushButton[0], SIGNAL(clicked()),
this, SLOT(timerStart()));
connect(pushButton[1], SIGNAL(clicked()),
this, SLOT(clearHostInfo()));
connect(timer, SIGNAL(timeout()),
this, SLOT(timerTimeOut()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::timerStart()
{
/* 清空文本 */
textBrowser->clear();
/* 定時(shí)1s */
timer->start(1000);
}
void MainWindow::timerTimeOut()
{
/* 顯示本機(jī)信息 */
showHostInfo();
/* 停止定時(shí)器 */
timer->stop();
}
QString MainWindow::getHostInfo()
{
/* 通過QHostInfo的localHostName函數(shù)獲取主機(jī)名稱 */
QString str = "主機(jī)名稱:" + QHostInfo::localHostName() + "\n";
/* 獲取所有的網(wǎng)絡(luò)接口,
* QNetworkInterface類提供主機(jī)的IP地址和網(wǎng)絡(luò)接口的列表 */
QList<QNetworkInterface> list
= QNetworkInterface::allInterfaces();
/* 遍歷list */
foreach (QNetworkInterface interface, list) {
str+= "網(wǎng)卡設(shè)備:" + interface.name() + "\n";
str+= "MAC地址:" + interface.hardwareAddress() + "\n";
/* QNetworkAddressEntry類存儲(chǔ)IP地址子網(wǎng)掩碼和廣播地址 */
QList<QNetworkAddressEntry> entryList
= interface.addressEntries();
/* 遍歷entryList */
foreach (QNetworkAddressEntry entry, entryList) {
/* 過濾IPv6地址,只留下IPv4 */
if (entry.ip().protocol() ==
QAbstractSocket::IPv4Protocol) {
str+= "IP 地址:" + entry.ip().toString() + "\n";
str+= "子網(wǎng)掩碼:" + entry.netmask().toString() + "\n";
str+= "廣播地址:" + entry.broadcast().toString() + "\n\n";
}
}
}
/* 返回網(wǎng)絡(luò)信息 */
return str;
}
void MainWindow::showHostInfo()
{
/* 獲取本機(jī)信息后顯示到textBrowser */
textBrowser->insertPlainText(getHostInfo());
}
void MainWindow::clearHostInfo()
{
/* 判斷textBrowser是否為空,如果不為空則清空文本 */
if (!textBrowser->toPlainText().isEmpty())
/* 清空文本 */
textBrowser->clear();
}
? ? ? ??首先,通過 QHostInfo 的 localHostName 函數(shù)獲取主機(jī)名稱。
? ? ? ? ?然后通過 QNetworkInterface::allInterfaces()獲取網(wǎng)絡(luò)接口列表 list 類存儲(chǔ) IP 地址子網(wǎng)掩碼和廣播地址。如果我們用 qDebug()函數(shù)打印出 list,可以發(fā)現(xiàn)獲取了所有的網(wǎng)絡(luò)信息。 而我們要提取網(wǎng)絡(luò)里面的網(wǎng)絡(luò)信息使用 QNetworkAddressEntry。
? ? ? ? 再用QNetworkAddressEntry 從 interface 接口里使用函數(shù) addressEntries(), 獲取所有的條目。就可以使用 QNetworkAddressEntry 的對(duì)象 entry 獲取 IP 地址子網(wǎng)掩碼和廣播地址。
????????因?yàn)楂@取的entries在一個(gè) QNetworkInterface下可能有兩個(gè)IP,分別是ipv4 和 ipv6。這里使用ip().protocol()來判斷協(xié)議的類型,只留下 ipv4 類型的信息。篩選信息在我們寫程序常常需要的。
程序運(yùn)行效果
????????點(diǎn)擊獲取本機(jī)信息,在文本瀏覽框內(nèi)就打印出本機(jī)的網(wǎng)絡(luò)信息(包括了主機(jī)名,網(wǎng)卡名, ip 地址等)。這里因?yàn)檫^濾掉了 IPv6 的信息。通常一個(gè)網(wǎng)卡有兩個(gè) ip 地址,一個(gè)是 ipv4,另一 個(gè)是 ipv6 的地址。下面的網(wǎng)卡設(shè)備 lo,是本地回環(huán)網(wǎng)卡。另一個(gè) ens33 是虛擬機(jī)的網(wǎng)卡,由 VMware 虛擬出來的。點(diǎn)擊清空文本信息會(huì)清空文本瀏覽框里的網(wǎng)絡(luò)信息。
?文章來源:http://www.zghlxwxcb.cn/news/detail-563526.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-563526.html
到了這里,關(guān)于【嵌入式Qt開發(fā)入門】Qt如何網(wǎng)絡(luò)編程——獲取本機(jī)的網(wǎng)絡(luò)信息的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!