使当前的QNetworkInterface处于活动状态并连接到Internet [英] Get current QNetworkInterface active and connected to the internet

查看:392
本文介绍了使当前的QNetworkInterface处于活动状态并连接到Internet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想激活当前的网络接口并连接到Internet.

I would like get the current network interface active and connected to the internet.

实际上,我可以检查网络是否正常运行,以及是否不是环回网络.

Actually, I can check if a network is up and if is not a loop back network.

  foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
    {
        if (interface.flags().testFlag(QNetworkInterface::IsUp) && !interface.flags().testFlag(QNetworkInterface::IsLoopBack))
            foreach (QNetworkAddressEntry entry, interface.addressEntries())
            {
            if ( interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString().contains("."))
                items << interface.name() + " "+ entry.ip().toString() +" " + interface.hardwareAddress();
        }

结果:

"en1 3.3.3.52 D4:9A:20:61:1F:72" 
"vmnet1 192.168.169.1 00:50:56:C0:00:01" 
"vmnet8 192.168.210.1 00:50:56:C0:00:08"

实际上它可以工作,但我也找到了VM接口. 而且我只想选择WLAN接口和以太网接口.

In fact it works but I found also VM interfaces. And I want to only select WLAN interfaces and Ethernet interfaces.

推荐答案

很抱歉,我想重提一个老问题,但是我只是自己考虑一下,并想出了一个解决方案:

Sorry to revive an old question, but I was just pondering this myself and came up with a solution:

QList<QString> possibleMatches;
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
if ( !ifaces.isEmpty() )
{
  for(int i=0; i < ifaces.size(); i++)
  {
    unsigned int flags = ifaces[i].flags();
    bool isLoopback = (bool)(flags & QNetworkInterface::IsLoopBack);
    bool isP2P = (bool)(flags & QNetworkInterface::IsPointToPoint);
    bool isRunning = (bool)(flags & QNetworkInterface::IsRunning);

    // If this interface isn't running, we don't care about it
    if ( !isRunning ) continue;
    // We only want valid interfaces that aren't loopback/virtual and not point to point
    if ( !ifaces[i].isValid() || isLoopback || isP2P ) continue;
    QList<QHostAddress> addresses = ifaces[i].allAddresses();
    for(int a=0; a < addresses.size(); a++)
    {
      // Ignore local host
      if ( addresses[a] == QHostAddress::LocalHost ) continue;

      // Ignore non-ipv4 addresses
      if ( !addresses[a].toIPv4Address() ) continue;

      QString ip = addresses[a].toString();
      if ( ip.isEmpty() ) continue;
      bool foundMatch = false;
      for (int j=0; j < possibleMatches.size(); j++) if ( ip == possibleMatches[j] ) { foundMatch = true; break; }
      if ( !foundMatch ) { possibleMatches.push_back( ip ); qDebug() << "possible address: " << ifaces[i].humanReadableName() << "->" << ip; }
    }
  }
}
// Now you can peek through the entries in possibleMatches
// With VMWare installed, I get two entries, and the first one is the correct one.
// If you wanted to test which one has internet connectivity, try creating a tcp
// connection to a known internet service (e.g. google.com) and if the connection
// is successful, check the following on the tcp connection
/*
if ( socket->localAddress().toIPv4Address() )
{
  for(int c=0; c < possibleMatches.size(); c++) if ( socket->localAddress().toString() == possibleMatches[c] ) { qDebug() << "Your LAN IP:" << possibleMatches[c]; }
}
*/

您可能想使其更加健壮,因此代码可以同时跟踪接口和ip地址,但这可能是不必要的,因为我敢肯定一个接口最多只能容纳一个接口ip地址,并且任何两个接口都不能具有相同的IP地址(如果我对此假设有误,请纠正我).

You may want to make it a little more robust, so the code could keep track of both the interface and the ip address, but that may be unnecessary, since I'm pretty sure an interface can't hold more than one ip address, and no two interfaces can have the same IP address (correct me if I'm wrong on that assumption).

这篇关于使当前的QNetworkInterface处于活动状态并连接到Internet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆