Qt网络编程基础
Qt直接提供网络编程模块,基于TCP/IP客户端和服务器相关各种类。TCP通信(QTcpSocket/QTcpServer)。UDP通信(QUdpSocket)。还有部分实现HTTP、FTP等网络协议的高级类。如QNetworkRequest/QNetworkAccessManager等。
我们开发过程中,UDP、TCP、HTTP 通信类等,必须在程序中引用 Qt 网络模块,项目配置文件的一条配置语句:QT +=network
QHostInfol类:QHostInfo类为主机名查找提供静态函数,主要用来查询主机信息、包括主机名、ip地址、DNS域名等信息。
QNetworkInterface类,主要用于获取主机所有IP地址和网络接口列表信息。
实例程序
#include "gethostnameipinfo.h"
#include "ui_gethostnameipinfo.h"
#include <QMessageBox>
GetHostNameIPInfo::GetHostNameIPInfo(QWidget *parent)
: QDialog(parent)
, ui(new Ui::GetHostNameIPInfo)
{
ui->setupUi(this);
}
GetHostNameIPInfo::~GetHostNameIPInfo()
{
delete ui;
}
void GetHostNameIPInfo::getHostNameIP()
{
// 获取主机名称
QString StrLocalHostName = QHostInfo::localHostName();
ui->lineEditHostName->setText(StrLocalHostName);
// 根据主机名称获取对应的IP地址
QString StrLocalIpAddress;
QHostInfo hostinfo = QHostInfo::fromName(StrLocalHostName);
QList<QHostAddress> ipaddresslist = hostinfo.addresses();
if(!ipaddresslist.isEmpty()){
for(int i=0;i<ipaddresslist.size();i++){
QHostAddress addresshost = ipaddresslist.at(i);
if(QAbstractSocket::IPv4Protocol == addresshost.protocol()){
StrLocalIpAddress = addresshost.toString();
break;
}
}
}
ui->lineEditIPAddr->setText(StrLocalIpAddress);
}
void GetHostNameIPInfo::on_GetHostNameIP_btn_clicked()
{
getHostNameIP();
}
void GetHostNameIPInfo::on_GetHostInfo_btn_clicked()
{
QString strTemp = "";
QList<QNetworkInterface> netlist = QNetworkInterface::allInterfaces();
for(int i=0;i<netlist.size();i++){
QNetworkInterface interface = netlist.at(i);
strTemp = strTemp + QString::fromLocal8Bit("设备名称:") + interface.name() + "\n";
strTemp = strTemp + QString::fromLocal8Bit("硬件地址:") + interface.hardwareAddress() + "\n";
QList<QNetworkAddressEntry> entrylist = interface.addressEntries();
for(int j=0;j<entrylist.size();j++){
QNetworkAddressEntry entry = entrylist.at(j);
strTemp = strTemp + QString::fromLocal8Bit("\tip地址:") + entry.ip().toString() + "\n";
strTemp = strTemp + QString::fromLocal8Bit("\t子网掩码:") + entry.netmask().toString() + "\n";
strTemp = strTemp + QString::fromLocal8Bit("\t广播地址:") + entry.broadcast().toString() + "\n";
}
}
QMessageBox::information(this,QString::fromLocal8Bit("主机所有信息"),strTemp,QMessageBox::Ok);
}
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
QString Dialog::ProtocolTypeName(QAbstractSocket::NetworkLayerProtocol protocol)
{
switch(protocol){
case QAbstractSocket::IPv4Protocol:
return "IPv4 Protocol";
case QAbstractSocket::IPv6Protocol:
return "IPv6 Protocol";
case QAbstractSocket::AnyIPProtocol:
return "Any IP Protocol";
default:
return "Unknown Network Layer Protocol";
}
}
void Dialog::LookupHostInfoFunc(const QHostInfo &host)
{
QList<QHostAddress> addresslist = host.addresses();
for(int i=0;i<addresslist.size();i++){
QHostAddress address = addresslist.at(i);
ui->plainTextEdit_DomainIP->append(QString::fromLocal8Bit("协议类型:")+ProtocolTypeName(address.protocol()));
ui->plainTextEdit_DomainIP->append(QString::fromLocal8Bit("本地IP地址:")+address.toString());
}
}
void Dialog::on_ClearData_clicked()
{
ui->plainTextEdit_DomainIP->clear();
}
void Dialog::on_GetDomainIP_clicked()
{
QString strhostname = ui->lineEdit_InputUrl->text();
ui->plainTextEdit_DomainIP->append(QString::fromLocal8Bit("你所查询主机信息:")+strhostname);
QHostInfo::lookupHost(strhostname,this,SLOT(LookupHostInfoFunc(QHostInfo)));
}
标签:知识,Qt,QHostInfo,void,编程,ui,QString,Dialog,GetHostNameIPInfo From: https://blog.csdn.net/m0_59591650/article/details/144477492