Get current QNetworkInterface active and connected to the internet

半腔热情 提交于 2019-12-22 08:57:15

问题


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();
        }

Results:

"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"

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


回答1:


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]; }
}
*/

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).




回答2:


I know the question is old but I'm working on something similar.

Here is my solution:

  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(".") && !interface.humanReadableName().contains("VM"))
                items << interface.name() + " "+ entry.ip().toString() +" " + interface.hardwareAddress();
        }

I have just added the

&& !interface.humanReadableName().contains("VM")

to your second if statement. Now it doesn't list adapters containing the string "vm".

Hope this helps for other people too.




回答3:


Use the enum QNetworkInterface::InterfaceFlag to get this information.

QNetworkInterface::IsUp         0x1 the network interface is active
QNetworkInterface::IsRunning    0x2 the network interface has resources allocated
QNetworkInterface::CanBroadcast 0x4 the network interface works in broadcast mode
QNetworkInterface::IsLoopBack   0x8 the network interface is a loopback interface: that is, it's a virtual interface whose destination is the host computer itself
QNetworkInterface::IsPointToPoint   0x10    the network interface is a point-to-point interface: that is, there is one, single other address that can be directly reached by it.
QNetworkInterface::CanMulticast 0x20    the network interface supports multicasting

From Qt 4.7 documentation.




回答4:


Set up a TCP connection using QTcpSocket. Next, get its .localAddress. You might find that different parts of the Internet correspond to different local adresses.



来源:https://stackoverflow.com/questions/5572263/get-current-qnetworkinterface-active-and-connected-to-the-internet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!