qt检测网络连接状态【只能检测和路由器的连接,不能测试到外网的连接】

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 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            {
                    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            {
                    qDebug() << iface.name()<< "isNotConnected" << endl;
            }
        }
    }

}

你可能感兴趣的:(qt检测网络连接状态【只能检测和路由器的连接,不能测试到外网的连接】)