QT学习_网络编程_主机信息

Posted Leslie X徐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QT学习_网络编程_主机信息相关的知识,希望对你有一定的参考价值。

网络编程

准备

在pro文件加入
Qt += network

主机信息查询

协议信息参数

enum NetworkLayerProtocol { IPv4Protocol, IPv6Protocol, UnknownNetworkLayerProtocol }

1.用到的类

  • QHostInfo
部分公共函数:

QHostInfo ( int id = -1 )
QHostInfo ( const QHostInfo & other )

QList<QHostAddress> 	addresses () const
HostInfoError 	error () const
QString 	errorString () const
QString 	hostName () const
int 			lookupId () const

静态函数:

void 				abortHostLookup ( int id )
QHostInfo 	fromName ( const QString & name )
QString 		localDomainName ()
QString 		localHostName ()
int 				lookupHost ( const QString & name, QObject * receiver, const char * member )


  • QNetworkInterface
部分公共函数:

QNetworkInterface ( const QNetworkInterface & other )

QList<QNetworkAddressEntry> 	addressEntries () const
InterfaceFlags 	flags () const
QString 			hardwareAddress () const
QString 			humanReadableName () const
int 					index () const
bool 					isValid () const
QString 			name () const


部分静态函数:

QList<QHostAddress> 			allAddresses ()
QList<QNetworkInterface> 	allInterfaces ()

获取主机信息

在这里插入图片描述

1.QHostInfo获取主机信息:

void Widget::on_btn_getlocal_clicked()
{
    //获取主机信息
    QString hostname = QHostInfo::localHostName();//本地主机名
    ui->plainTextEdit->appendPlainText("LocalHost"+hostname);
    QHostInfo hostInfo = QHostInfo::fromName(hostname);//本机IP地址

    QList<QHostAddress> addList = hostInfo.addresses();//IP地址列表
    if(!addList.isEmpty())
        for (int i=0;i<addList.count();++i) {
            QHostAddress aHost = addList.at(i);//每一项是一个QHostAddress
            bool show = ui->checkBox->isChecked();
            if(show)
                show = (QAbstractSocket::IPv4Protocol == aHost.protocol());//判断该地址是不是使用IPv4协议
            else
                show = true;
            if(show){
                ui->plainTextEdit->appendPlainText("protocol:"+protocolName(aHost.protocol()));
                ui->plainTextEdit->appendPlainText("hostIP:"+aHost.toString());
                ui->plainTextEdit->appendPlainText("");
            }
        }
}

QString Widget::protocolName(QAbstractSocket::NetworkLayerProtocol protocol)
{
    switch (protocol) {
    case QAbstractSocket::IPv4Protocol:
    return "IPv4 Protocol";
    case QAbstractSocket::IPv6Protocol:
    return "IPv6 Protocol";
    case QAbstractSocket::UnknownNetworkLayerProtocol:
    return "unknown Protocol";
    default:
        return "total unknown Protocol";
    }
}

2.QHostInfo根据域名查找主机地址信息
主要使用QHostInfo::lookupHost函数,可以根据主机名、域名或IP地址查找主机的地址信息

void Widget::on_btn_getDNS_clicked()
{//查找主机信息
    QString hostname = ui->lineEdit->text();
    ui->plainTextEdit->appendPlainText("finding: "+hostname+"...");
    QHostInfo::lookupHost(hostname,this,SLOT(lookedUPHostInfo(QHostInfo)));
}

void Widget::lookedUPHostInfo(const QHostInfo &host)
{//查找主机信息的槽函数
    QList<QHostAddress> addList = host.addresses();
    if(!addList.isEmpty())
        for (int i=0;i<addList.count();++i) {
            QHostAddress aHost = addList.at(i);//每一项是一个QHostAddress
            bool show = ui->checkBox->isChecked();
            if(show)
                show = (QAbstractSocket::IPv4Protocol == aHost.protocol());//判断该地址是不是使用IPv4协议
            else
                show = true;
            if(show){
                ui->plainTextEdit->appendPlainText("protocol:"+protocolName(aHost.protocol()));
                ui->plainTextEdit->appendPlainText(aHost.toString());
            }
        }
    ui->plainTextEdit->appendPlainText("\\n");
}

3.QNetworkInterface使用
QNetworkInterface::allAddresses()使用,使用方法和QHostInfo::addresses()类似

void Widget::on_btn_addresses_clicked()
{
    //与on_btn_getlocal_clicked类似
    QList<QHostAddress> addList = QNetworkInterface::allAddresses();
    if(!addList.isEmpty())
        for (int i=0;i<addList.count();++i) {
            QHostAddress aHost = addList.at(i);
            bool show = ui->checkBox->isChecked();
            if(show)
                show = (QAbstractSocket::IPv4Protocol == aHost.protocol());
            else
                show = true;
            if(show){
                ui->plainTextEdit->appendPlainText("protocol:"+protocolName(aHost.protocol()));
                ui->plainTextEdit->appendPlainText("hostIP:"+aHost.toString());
                ui->plainTextEdit->appendPlainText("");
            }
        }
}

4.QNetworkInterface使用
QNetworkInterface::allInterfaces()获得应用程序所在主机的所有网络接口
其中有一个类QNetworkAddressEntry包含了一个网络接口的IP地址,子网掩码和广播地址

void Widget::on_btn_interfaces_clicked()
{
    QList<QNetworkInterface> addList = QNetworkInterface::allInterfaces();
    if(!addList.isEmpty())
    for (int i=0;i<addList.count();++i) {
        QNetworkInterface aInterface = addList.at(i);
        if(!aInterface.isValid()) continue;
        ui->plainTextEdit->appendPlainText("HardwareName:"+ aInterface.humanReadableName());
        ui->plainTextEdit->appendPlainText("HardwareIP:"+ aInterface.hardwareAddress());

        QList<QNetworkAddressEntry> entryList = aInterface.addressEntries();
        for (int j=0;j<entryList.count();j++) {
            QNetworkAddressEntry aEntry = entryList.at(j);
            ui->plainTextEdit->appendPlainText("IPAddress: " + protocolName(aEntry.ip().protocol()));
            ui->plainTextEdit->appendPlainText("netmask: " + aEntry.netmask().toString());
            ui->plainTextEdit->appendPlainText("broadcast: "+ aEntry.broadcast().toString() + "\\n");
        }
        ui->plainTextEdit->appendPlainText("");
    }
}

以上是关于QT学习_网络编程_主机信息的主要内容,如果未能解决你的问题,请参考以下文章

QT学习_青云客网络聊天

Qt获取本机网络信息

基于Qt的OpenGL可编程管线学习- 探照灯

基于Qt的OpenGL可编程管线学习- 探照灯

基于Qt的OpenGL可编程管线学习- 探照灯

Qt (Creator) 与 WinSocks (ws2_32)