2011-04-06 382 views
4

我想讓當前的網絡接口處於活動狀態並連接到互聯網。獲取當前的QNetworkInterface激活並連接到互聯網

其實,我可以檢查網絡是否啓動,如果不是環回網絡。

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" 

事實上,它的工作,但我也發現了虛擬機的接口。 而我不想只選擇wlan接口和以太網接口

+0

您發佈的代碼塊有什麼問題? – 2011-04-06 20:04:58

+0

@Adam:我猜他不知道接口是否處於活動狀態。 – 2011-04-06 20:06:36

+0

如果你只想要WLAN和以太網接口,那麼帶VPN的人會感到失望。通常這是他們與互聯網的唯一聯繫。 – MSalters 2011-04-07 08:40:38

回答

1

使用enum QNetworkInterface::InterfaceFlag來獲取此信息。

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 

Qt 4.7 documentation

+0

其實這個代碼顯示所有接口活動,但我不想選擇physiq接口,而不是vm接口你有沒有理念? – menkel 2011-04-06 20:31:26

-1

使用QTcpSocket設置TCP連接。接下來,得到它的.localAddress。您可能會發現互聯網的不同部分對應於不同的本地地址。

5

對不起恢復一個老問題,但我只是琢磨這個自己,想出了一個解決方案:

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地址(如果我錯了,請糾正我在這個假設下)。

2

我知道這個問題很老,但我正在做類似的工作。

這裏是我的解決方案:

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

我剛添加的

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

你的第二個if語句。現在它不會列出包含字符串「vm」的適配器。

希望這對其他人也有幫助。