首页 > 代码库 > qt检测网络连接状态【只能检测和路由器的连接,不能测试到外网的连接】
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检测网络连接状态【只能检测和路由器的连接,不能测试到外网的连接】