Qt5獲取網卡/IP等信息

參考網址:http://blog.csdn.net/wjs1033/article/details/22697063網絡

 

一、環境 Win7x6四、Qt5.5.1(x86)、vs2013_ultimate_up5(x86)app

  1.一、?.h / ?.cpp 都保存成「UTF-8 + BOM」格式(這樣,源碼裏面中文/特殊符號,使用中文註釋,就不會有 error或warnning了)函數

  1.二、?.h / ?.cpp 都保存成「UTF-8 + BOM」格式 的話,qDebug()輸出中文的時候 全是亂碼...oop

      ?.h / ?.cpp 都保存成「UTF-8」           格式 的話,qDebug()輸出中文的時候 小部分是亂碼...ui

    ZC: 暫時 先將 ?.h / ?.cpp 都保存成「UTF-8」格式,之後再研究 這些字符串編碼的 中文亂碼的事情吧...this

二、代碼:編碼

  2.一、?.prospa

#-------------------------------------------------
#
# Project created by QtCreator 2016-11-14T16:10:00
#
#-------------------------------------------------

QT       += core gui    \
            network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = NetworkInterfaceZ
TEMPLATE = app


SOURCES += main.cpp\
        MainWindow.cpp

HEADERS  += MainWindow.h

FORMS    += MainWindow.ui

  2.二、MainWindow.h.net

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QHostInfo>

// http://blog.csdn.net/wjs1033/article/details/22697063

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pbtnMsg01_clicked();

    void on_pbtnMsg02_clicked();

    void on_pbtnMsg03_clicked();

    void on_pbtnMsg04_clicked();

    void on_pbtnMsg05_clicked();

private:
    Ui::MainWindow *ui;

public slots:
    void LookupHostZ(const QHostInfo &_hostInfo);
};

#endif // MAINWINDOW_H

  2.三、MainWindow.cpp3d

#include "MainWindow.h"
#include "ui_MainWindow.h"

#include <QNetworkInterface>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

// 枚舉本機的網絡鏈接並獲取其屬性
void MainWindow::on_pbtnMsg01_clicked()
{
    int i=0, j=0;
    QList<QNetworkInterface> networkInterface = QNetworkInterface::allInterfaces();
    for (QList<QNetworkInterface>::const_iterator it = networkInterface.constBegin(); it != networkInterface.constEnd(); ++it)
    {
        qDebug() << "[" << i << "] : " << (*it).name();
        qDebug() << "  " << (*it).humanReadableName();
        qDebug() << "  " << (*it).hardwareAddress();

        //獲取鏈接地址列表
        QList<QNetworkAddressEntry> addressEntriesList = (*it).addressEntries();
        for (QList<QNetworkAddressEntry>::const_iterator jIt = addressEntriesList.constBegin(); jIt != addressEntriesList.constEnd(); ++jIt)
        {
            qDebug() << "\t(" << j << ") :";
            //輸出 ip
            qDebug() << "\t\tIP : " <<(*jIt).ip().toString();
            //輸出 netmask
            qDebug() << "\t\tnetmask(子網掩碼) : " << (*jIt).netmask().toString();
            qDebug() << "\t\tBroadcast(廣播地址) : "<< (*jIt).broadcast().toString();
            qDebug() << "";
            j ++;
        }
        i ++;
    }
}

// 只枚舉ip地址的簡潔方式
void MainWindow::on_pbtnMsg02_clicked()
{
    QList<QHostAddress> list = QNetworkInterface::allAddresses();
    {
        foreach(QHostAddress address,list)
        {
            if(address.protocol() == QAbstractSocket::IPv4Protocol)
                qDebug() << address.toString();
        }
    }
}

// 獲取本機主機名 及 IPv4地址
void MainWindow::on_pbtnMsg03_clicked()
{
    QString strLocalHostName = QHostInfo::localHostName(); // 獲取主機名
    qDebug() << "本地主機名 : " << strLocalHostName;

    QHostInfo info = QHostInfo::fromName(strLocalHostName);// 根據上邊得到的主機名來獲取本機的信息
    // QHostInfo的address函數獲取本機ip地址
    // QHostAddress類是管理ip地址的類,全部的ip都歸這個類管理。
    foreach (QHostAddress address, info.addresses())
    {
        if(address.protocol() == QAbstractSocket::IPv4Protocol)//只取ipv4協議的地址
            qDebug() << "IP(IPv4Protocol) : " << address.toString();
    }
}

// 經過 主機名 獲取IP地址
void MainWindow::on_pbtnMsg04_clicked()
{
    // 以主機名獲取ip (會調用slot函數LookupHostZ)
    QHostInfo::lookupHost("www.baidu.com", this, SLOT(LookupHostZ(QHostInfo)));
}

// 經過IP地址 反向查詢 主機名
void MainWindow::on_pbtnMsg05_clicked()
{
    // 經過ip地址獲取主機名 (會調用slot函數LookupHostZ)
    QHostInfo::lookupHost("192.168.1.201", this, SLOT(LookupHostZ(QHostInfo)));
}

// 響應QHostInfo::lookupHost(...) 的slot函數
void MainWindow::LookupHostZ(const QHostInfo &_hostInfo)
{
    // 輸出IPv4的IP地址
    foreach (QHostAddress address, _hostInfo.addresses())
    {
        if(address.protocol() == QAbstractSocket::IPv4Protocol)//只取ipv4協議的地址
            qDebug() << "LookupHostZ - IP(IPv4Protocol) : " << address.toString();
    }

    // 輸出主機名
    qDebug() << _hostInfo.hostName();
}

  2.四、MainWindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>518</width>
    <height>354</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pbtnMsg01">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>10</y>
      <width>211</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>枚舉本機的網絡鏈接並獲取其屬性</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pbtnMsg02">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>40</y>
      <width>211</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>只枚舉ip地址的簡潔方式</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pbtnMsg03">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>90</y>
      <width>211</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>獲取本機主機名及IPv4地址</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pbtnMsg04">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>140</y>
      <width>211</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>經過主機名獲取IP地址</string>
    </property>
   </widget>
   <widget class="QPushButton" name="pbtnMsg05">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>170</y>
      <width>211</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>經過IP地址反向查詢主機名</string>
    </property>
   </widget>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

  2.五、界面截圖

三、程序運行輸出:

  3.一、枚舉本機的網絡鏈接並獲取其屬性

[ 0 ] :  "{7558E85A-8386-4A5F-A5BF-4A4F22792AA4}"
   "無線網絡鏈接"
   "34:23:87:61:DF:31"
    ( 0 ) :
        IP :  "fe80::a4d3:7dfd:62cf:558d%14"
        netmask(子網掩碼) :  "ffff:ffff:ffff:ffff::"
        Broadcast(廣播地址) :  ""

    ( 1 ) :
        IP :  "169.254.85.141"
        netmask(子網掩碼) :  ""
        Broadcast(廣播地址) :  ""

[ 1 ] :  "{3B25081D-362E-42E8-8FDF-B6D5A99824CF}"
   "本地鏈接"
   "28:D2:44:40:5B:C1"
    ( 2 ) :
        IP :  "fe80::e58e:6f0e:df37:e8ed%12"
        netmask(子網掩碼) :  "ffff:ffff:ffff:ffff::"
        Broadcast(廣播地址) :  ""

    ( 3 ) :
        IP :  "192.168.1.233"
        netmask(子網掩碼) :  "255.255.255.0"
        Broadcast(廣播地址) :  "192.168.1.255"

[ 2 ] :  "{B5D6033F-8002-4428-80A3-1974993AF12A}"
   "Bluetooth 網絡鏈接"
   "34:23:87:61:DF:32"
    ( 4 ) :
        IP :  "fe80::e0b7:f552:784e:9fe%11"
        netmask(子網掩碼) :  "ffff:ffff:ffff:ffff::"
        Broadcast(廣播地址) :  ""

    ( 5 ) :
        IP :  "169.254.9.254"
        netmask(子網掩碼) :  ""
        Broadcast(廣播地址) :  ""

[ 3 ] :  "{8376C30F-73E1-4029-B465-AB5449D3812E}"
   "VMware Network Adapter VMnet1"
   "00:50:56:C0:00:01"
    ( 6 ) :
        IP :  "fe80::29c7:6a:97d7:bb0f%19"
        netmask(子網掩碼) :  "ffff:ffff:ffff:ffff::"
        Broadcast(廣播地址) :  ""

    ( 7 ) :
        IP :  "192.168.131.1"
        netmask(子網掩碼) :  "255.255.255.0"
        Broadcast(廣播地址) :  "192.168.131.255"

[ 4 ] :  "{1D0B26ED-F9CB-43A4-AD64-202A6D727BFA}"
   "VMware Network Adapter VMnet8"
   "00:50:56:C0:00:08"
    ( 8 ) :
        IP :  "fe80::a567:53d5:a0b5:cd63%20"
        netmask(子網掩碼) :  "ffff:ffff:ffff:ffff::"
        Broadcast(廣播地址) :  ""

    ( 9 ) :
        IP :  "192.168.181.1"
        netmask(子網掩碼) :  "255.255.255.0"
        Broadcast(廣播地址) :  "192.168.181.255"

[ 5 ] :  "{846EE342-7039-11DE-9D20-806E6F6E6963}"
   "Loopback Pseudo-Interface 1"
   ""
    ( 10 ) :
        IP :  "::1"
        netmask(子網掩碼) :  "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
        Broadcast(廣播地址) :  ""

    ( 11 ) :
        IP :  "127.0.0.1"
        netmask(子網掩碼) :  ""
        Broadcast(廣播地址) :  ""

[ 6 ] :  "{DA5C1007-9FC1-4637-AFBF-CFC713356DDA}"
   "isatap.{3B25081D-362E-42E8-8FDF-B6D5A99824CF}"
   "00:00:00:00:00:00:00:E0"
    ( 12 ) :
        IP :  "fe80::5efe:c0a8:1e9%15"
        netmask(子網掩碼) :  "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
        Broadcast(廣播地址) :  ""

[ 7 ] :  "{B6A0553B-7196-4747-87B8-D1307AA54F7E}"
   "Teredo Tunneling Pseudo-Interface"
   "00:00:00:00:00:00:00:E0"
    ( 13 ) :
        IP :  "fe80::100:7f:fffe%13"
        netmask(子網掩碼) :  "ffff:ffff:ffff:ffff::"
        Broadcast(廣播地址) :  ""

[ 8 ] :  "{B61A7FDB-E0C1-42CC-A324-54F35789A245}"
   "isatap.{8376C30F-73E1-4029-B465-AB5449D3812E}"
   "00:00:00:00:00:00:00:E0"
    ( 14 ) :
        IP :  "fe80::5efe:c0a8:8301%16"
        netmask(子網掩碼) :  "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
        Broadcast(廣播地址) :  ""

[ 9 ] :  "{6B68472C-AA56-44A8-A9AE-FFEF1A4ED978}"
   "isatap.{7558E85A-8386-4A5F-A5BF-4A4F22792AA4}"
   "00:00:00:00:00:00:00:E0"
[ 10 ] :  "{BAAD8784-EFFA-4C76-AE9E-8074A9965DBA}"
   "isatap.{B5D6033F-8002-4428-80A3-1974993AF12A}"
   "00:00:00:00:00:00:00:E0"
[ 11 ] :  "{7F3F01D3-A615-4238-8453-632DCB4C13C6}"
   "isatap.{1D0B26ED-F9CB-43A4-AD64-202A6D727BFA}"
   "00:00:00:00:00:00:00:E0"
    ( 15 ) :
        IP :  "fe80::5efe:c0a8:b501%21"
        netmask(子網掩碼) :  "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
        Broadcast(廣播地址) :  ""

  3.二、只枚舉ip地址的簡潔方式

"169.254.85.141"
"192.168.1.233"
"169.254.9.254"
"192.168.131.1"
"192.168.181.1"
"127.0.0.1"

  3.三、獲取本機主機名 及 IPv4地址 (這裏的 控制檯輸出 中文會有亂碼)

"本地主機???: " "33-PC"
IP(IPv4Protocol) :  "192.168.1.233"
IP(IPv4Protocol) :  "192.168.131.1"
IP(IPv4Protocol) :  "192.168.181.1"

  3.四、經過 主機名 獲取IP地址

LookupHostZ - IP(IPv4Protocol) :  "180.97.33.108"
LookupHostZ - IP(IPv4Protocol) :  "180.97.33.107"
"www.baidu.com"

  3.五、經過IP地址 反向查詢 主機名 (這個執行的時候,用時較長)

LookupHostZ - IP(IPv4Protocol) :  "192.168.1.201"
"ZHEJIANG001"

  3.六、

 

四、獲取本機的 第一個IPv4地址的字符串 (Windows下)

  傳入參數:_strNetworkInterfaceHumanReadableName : 網絡接口卡的名稱(相似"無線網絡鏈接"、"本地鏈接"等)

QString Widget::getIP(QString _strNetworkInterfaceHumanReadableName)
{
    //QString str01 = QString::fromLocal8Bit("本地鏈接");

    QList<QNetworkInterface> networkInterface = QNetworkInterface::allInterfaces();
    foreach (QNetworkInterface intf, networkInterface)
    {
        bool b = false;
        if (_strNetworkInterfaceHumanReadableName == NULL)
            b = true;
        else
        {
            QString str = intf.humanReadableName();
            if (str.startsWith(_strNetworkInterfaceHumanReadableName, Qt::CaseInsensitive))
                b = true;
        }

        if (b)
        {
            QList<QNetworkAddressEntry> listNAE = intf.addressEntries();
            foreach (QNetworkAddressEntry nae, listNAE)
            {
                QHostAddress ha = nae.ip();
                if (ha.protocol() == QAbstractSocket::IPv4Protocol)
                    return ha.toString();
            }
        }
    }
    return NULL;
}

 

五、

相關文章
相關標籤/搜索