qt-c++ advanced 1-window, under linux, get all the network card ip information of the machine, and get the ip address according to the network card name.

Series Article Directory

For example: the first chapter is mainly to obtain the network card information or IP information of the local computer through qt-c++



foreword

Summarize the method of c++ to obtain the information of the local network card
Chapter 1: Applicable to windows operating system and linux operating system

  1. QHostAddressObtain the machine by parsing IPV4地址;
  2. Use QNetworkInterfaceparsing to get all gateway information ( IP地址(IPV4和IPV6), 子网掩码, 广播地址);

Continuously updating...


1. Obtain the IP information of the local network card

This chapter is mainly to obtain the network card information or IP information of the local computer through qt-c++

The method of obtaining all the local gateway IPs of windows
The method of obtaining the local IP of the specified network card name.

1.1 Obtaining the ip address method

  1. QHostAddressObtain the machine by parsing IPV4地址;
  2. Use QNetworkInterfaceparsing to get all gateway information ( IP地址(IPV4和IPV6), 子网掩码, 广播地址);
  3. According to the above method, it is concluded that according to the name of the local network card to obtain the correspondingIPV4地址

1.2 Code example

Provide 3 functions in widget.h and implement them in widget.cpp respectively, as follows:

void getHostIP(); //Get the ipv4 address of the machine
void getAllIP(); //Get all the network card information of the machine
void getDesignateIP(); //Get the ip information of the specified network card according to the network card name, for example, the following example gets VMware Network Adapter VMnet8the ip of the network card

Directory Structure
insert image description here

Code example:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
    
    
class Widget;
}

class Widget : public QWidget
{
    
    
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    
    void getHostIP();//获取本机ipv4地址
    void getAllIP();//获取本机所有网卡信息
    void getDesignateIP();//获取本机的制定ip信息

    ~Widget();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QList>
#include <QNetworkInterface>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    
    
    ui->setupUi(this);
    getHostIP();
    getAllIP();
}

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

void Widget::getDesignateIP()
{
    
    
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    //获取所有网络接口的列表
    foreach(QNetworkInterface interface,list)
    {
    
    
        QList<QNetworkAddressEntry> entryList = interface.addressEntries();
        //获取IP地址条目列表,每个条目中包含一个IP地址,一个子网掩码和一个广播地址
        if(interface.humanReadableName() == "VMware Network Adapter VMnet8")
        {
    
    
            foreach(QNetworkAddressEntry entry,entryList)
            {
    
    
                if(entry.ip()!=QHostAddress::LocalHost && entry.ip().toIPv4Address())
                {
    
    
                    //设备名
                    qDebug() << "Device: "<<interface.name();
                    qDebug() << "Device: "<<interface.humanReadableName();
                    //IP信息
                    qDebug()<<"IP Address: "<<entry.ip().toString();              
                }
            }
        }
    }
}

void Widget::getHostIP()
{
    
    
    QList<QHostAddress> list = QNetworkInterface::allAddresses();
    foreach (QHostAddress address, list)
    {
    
    
        if(address.protocol() == QAbstractSocket::IPv4Protocol)
            //我们使用IPv4地址
            qDebug()<<address.toString();
    }
     qDebug()<<endl;
}

void Widget::getAllIP()
{
    
    
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    //获取所有网络接口的列表
    foreach(QNetworkInterface interface,list)
    {
    
      //遍历每一个网络接口
        qDebug() << "Device: "<<interface.name();
        //设备名
        qDebug() << "HardwareAddress: "<<interface.hardwareAddress();
        //硬件地址
        QList<QNetworkAddressEntry> entryList = interface.addressEntries();
        //获取IP地址条目列表,每个条目中包含一个IP地址,一个子网掩码和一个广播地址
        foreach(QNetworkAddressEntry entry,entryList)
        {
    
    //遍历每一个IP地址条目
            qDebug()<<"IP Address: "<<entry.ip().toString();
            //IP地址
            qDebug()<<"Netmask: "<<entry.netmask().toString();
            //子网掩码
            qDebug()<<"Broadcast: "<<entry.broadcast().toString();
            //广播地址
        }
        qDebug()<<endl;
    }
}

Run the screenshot:
insert image description here

Reference link: https://www.cnblogs.com/liushui-sky/p/6479110.html


Summarize

Here is just a summary of the method of obtaining network card information with qt-c++, and a variety of methods for obtaining network card ip information in c and c++ will be summarized later, and it is being updated continuously...

Guess you like

Origin blog.csdn.net/weixin_55491446/article/details/129250040