qt检测网络连接状态只能检测和路由器的连接,不能测试到外网的连接
Posted oucaijun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qt检测网络连接状态只能检测和路由器的连接,不能测试到外网的连接相关的知识,希望对你有一定的参考价值。
#include <QCoreApplication>
#include <QDebug>
#include <QTextStream>
#include <QDir>
#include <QFile>
#include <QList>
#include <QThread>
#include <QtNetwork/QNetworkConfigurationManager>
#include <QtNetwork/QNetworkConfiguration>
#include <QtNetwork/QNetworkInterface>
#define ETH_IF_NAME "eth0"
#define ETH_IF_PPP0 "ppp0"
#define ETH_IF_PPP1 "ppp1"
#define ETH_IF_ETH1 "eth1"
#define ETH_IF_USB0 "usb0"
#define ETH_NAME_SERVER "nameserver 127.0.0.1"
static bool status[2]={0};
bool CheckNetInfo()
{
system("ifconfig eth0 up");
system("ifconfig usb0 up");
memset(status, 0 ,sizeof(status));
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
for (int i = 0; i < ifaces.count(); i++)
{
QNetworkInterface iface = ifaces.at(i);
if ( iface.flags().testFlag(QNetworkInterface::IsUp)
&& iface.flags().testFlag(QNetworkInterface::IsRunning)
&& !iface.flags().testFlag(QNetworkInterface::IsLoopBack)
&& ((iface.name() == ETH_IF_PPP0) ||(iface.name() == ETH_IF_PPP1)
||(iface.name() == ETH_IF_ETH1) ||(iface.name() == ETH_IF_USB0))
||(iface.name() == ETH_IF_NAME) ||(iface.name() == ETH_NAME_SERVER)
)
{
for (int j=0; j<iface.addressEntries().count(); j++)
{
qDebug() << iface.name()<< "isConnected" << endl;
if(iface.name() == ETH_IF_USB0)
status[0] = true;
else if(iface.name() == ETH_IF_NAME)
status[1] = true;
}
} else {
for (int j=0; j<iface.addressEntries().count(); j++)
{
qDebug() << iface.name()<< "isNotConnected" << endl;
}
}
}
}
以上是关于qt检测网络连接状态只能检测和路由器的连接,不能测试到外网的连接的主要内容,如果未能解决你的问题,请参考以下文章