如何在Qt中检查网络接口的类型



我的代码:

CheckNetwork.h

#include <QMainWindow>
#include <QDebug>
#include <QNetworkInterface>
#include <QNetworkAccessManager>
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkSession>
namespace Ui {
class CheckNetwork;
}
class CheckNetwork : public QMainWindow
{
    Q_OBJECT
public:
    explicit CheckNetwork(QWidget *parent = 0);
    ~CheckNetwork();
protected:
    void showEvent(QShowEvent *event);
private slots:
    void reply_finished();
    void network_configuration_manager_updateCompleted();
private:
    Ui::CheckNetwork *ui;
    QNetworkInterface *network_interface;
    QNetworkConfigurationManager *network_configuration_manager;
    QNetworkSession *network_session;
    QNetworkReply *reply;
};

CheckNetwork.cpp

#include "checknetwork.h"
#include "ui_checknetwork.h"

/*
Qt Creator 3.3.1 (opensource)
Qt 5.4.1 (MSVC 2010, 32 bit)
*/
CheckNetwork::CheckNetwork(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CheckNetwork)
{
    ui->setupUi(this);
}
CheckNetwork::~CheckNetwork()
{
    delete ui;
}
void CheckNetwork::showEvent(QShowEvent *event){
    ui->textEdit->append("-----------------");
    ui->textEdit->append("NetworkInterfaces");
    ui->textEdit->append("-----------------");
    ui->textEdit->append("UP AND RUNNING:");
    network_interface = new QNetworkInterface();
    foreach (QNetworkInterface interface, network_interface->allInterfaces()) {
        if((interface.flags() & QNetworkInterface::IsUp) && (interface.flags() & QNetworkInterface::IsRunning))
            ui->textEdit->append("t" + interface.name() + " " + interface.humanReadableName() + " " + interface.hardwareAddress());
    }

    ui->textEdit->append("-----------------------------");
    ui->textEdit->append("Network Configuration Manager");
    ui->textEdit->append("-----------------------------");
    ui->textEdit->append("ACTIVE:");
    network_configuration_manager = new QNetworkConfigurationManager(this);
    QObject::connect(network_configuration_manager, SIGNAL(updateCompleted()), this, SLOT(network_configuration_manager_updateCompleted()));
    network_configuration_manager->updateConfigurations();
}

void CheckNetwork::network_configuration_manager_updateCompleted(){
    foreach (QNetworkConfiguration configuration, network_configuration_manager->allConfigurations(QNetworkConfiguration::Active)) {
        ui->textEdit->append("t" + configuration.name() + " " + configuration.bearerTypeName() + " " + configuration.identifier());
    }
    ui->textEdit->append("DEFAULT:");
    ui->textEdit->append("t" + network_configuration_manager->defaultConfiguration().name() + " " + network_configuration_manager->defaultConfiguration().bearerTypeName());
    ui->textEdit->append("---------------");
    ui->textEdit->append("Network Session");
    ui->textEdit->append("---------------");
    network_session = new QNetworkSession(network_configuration_manager->defaultConfiguration(), this);
    network_session->open();
    if(network_session->isOpen()){
        ui->textEdit->append("tisOpen");
    }else{
        ui->textEdit->append("tnoOpen");
    }

    ui->textEdit->append("---------------");
    ui->textEdit->append("Network Manager");
    ui->textEdit->append("---------------");
    QNetworkAccessManager *network_access_manager = new QNetworkAccessManager(this);
    reply = network_access_manager->get(QNetworkRequest(QUrl("http://www.google.com")));
    QObject::connect(reply, SIGNAL(finished()), this, SLOT(reply_finished()));
}

void CheckNetwork::reply_finished(){
    ui->textEdit->append("REPLY: " + QString::number(reply->bytesAvailable()) + " byte(s)");
    ui->textEdit->append("REPLY DATAS: nn" + reply->readAll());
}

使用无线(WLAN)网卡的QTextEdit内容(win 8.1 64bit):

-----------------
NetworkInterfaces
-----------------
UP AND RUNNING:
    {UUID} Wi-Fi XX:XX:XX:XX:XX:XX
    {UUID} VirtualBox Host-Only Network XX:XX:XX:XX:XX:XX
    {UUID} Loopback Pseudo-Interface 1 
    {UUID} Local Network Connexion* 2 XX:XX:XX:XX:XX:XX:XX:XX
-----------------------------
Network Configuration Manager
-----------------------------
ACTIVE:
    NetworkWireslessName WLAN XXXXXXXXX
    Local Network Connexion* 2 Unknown XXXXXXXXX
    VirtualBox Host-Only Network Ethernet XXXXXXXXX
    Wi-Fi Ethernet XXXXXXXXX
DEFAULT:
    VirtualBox Host-Only Network Ethernet
---------------
Network Session
---------------
    isOpen
---------------
Network Manager
---------------
REPLY: 256 byte(s)
REPLY DATAS: 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.fr/?gfe_rd=cr&amp;ei=66-JVb_aJc7AULSAgcgE">here</A>.
</BODY></HTML>

使用有线(LAN)网卡(Win7 64位/Win10 32位)的QTextEdit内容:

-----------------
NetworkInterfaces
-----------------
UP AND RUNNING:
    {UUID} Local Network Connexion XX:XX:XX:XX:XX:XX
    {UUID} Loopback 
-----------------------------
Network Configuration Manager
-----------------------------
ACTIVE:
DEFAULT:
---------------
Network Session
---------------
    noOpen
---------------
Network Manager
---------------
REPLY: 258 byte(s)
REPLY DATAS: 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.fr/?gfe_rd=cr&amp;ei=ghGJVYecFcq50wX_u4CICg">here</A>.
</BODY></HTML>

我的问题是,我无法检查网络接口的类型,如果它是"有线或无线",则处于"在线"状态,以防止networkmanager在用户未连接的情况下自动启动连接网络(QNetworkConfigurationManager::CanStartAndStopInterfaces),因为通过此验证:

network_configuration_manager->defaultConfiguration();
if(!network_configuration_manager->isOnline()){        
        network_configuration_manager->deleteLater();
        return;
}

这非常适用于无线连接,但由于没有找到此检查的连接电缆的默认配置,networkmanager无法继续。。。换句话说,如果网络连接处于活动状态(而不是ping或查询谷歌等服务器),如何检查连接电缆。我也读到,对于一些以前的版本,类似的错误也存在。例如,在这个版本的Qt中,我在他的消息框中有错误,那么这会是一个错误吗?有人知道吗?感谢

您应该能够检查承载类型,看看它是否是wifi连接,即:

configuration.bearerType() == QNetworkConfiguration::BearerWLAN

我认为这个过程应该是:

  1. 从管理器获取所有网络配置。

  2. 对于每个活动配置,创建会话。

  3. 查看会话的界面。检查QNetworkInterface::flags()是否为QNetworkInterface::IsUp

通过这种方式,您也可以检查来自非默认会话的接口。

相关内容

  • 没有找到相关文章