Qt-A comprehensive example of obtaining network interface information

Source: Internet
Author: User

In the previous article, it is described that there are 4 commonly used classes related to acquiring native network information, namely: Qhostaddress, Qhostinfo, Qnetworkinterface and Qnetworkaddress Entry. These 4 classes are not described in detail, we mainly use these 4 classes of common methods to obtain information about the network interface.

1#include"mainwindow.h"2 3 #ifdef ir_func_test4#include <QDebug>5#include <QHostInfo>6#include <QHostAddress>7#include <QNetworkInterface>8#include <QNetworkAddressEntry>9 Ten #endif One  AMainwindow::mainwindow (Qwidget *parent) - : Qmainwindow (parent) - { the  - #ifdef ir_func_test -     //Get host name -QString Localhostname =qhostinfo::localhostname (); +Qdebug () <<"Host Name:"<<Localhostname; -  +     //get a list of IP addresses on this host AQhostinfo Hostinfo =Qhostinfo::fromname (localhostname); atQlist<qhostaddress> addrlist =hostinfo.addresses (); -Qdebug () <<"List of IP addresses on this host, total:"<<Addrlist.count (); -     foreach(qhostaddressvar, Addrlist) { -Qdebug () <<var. toString (); -     } -  in     //get a list of network interfaces for this machine -Qlist<qnetworkinterface> ifacelist =qnetworkinterface::allinterfaces (); to      for(inti =0; I < Ifacelist.count (); i++){ +Qnetworkinterfacevar=ifacelist.at (i); -Qdebug () << TR ("########## device%1 ############"). Arg (i); theQdebug () << TR ("Interface Name:") <<var. Humanreadablename (); *Qdebug () << TR ("Device Name:") <<var. Name (); $Qdebug () << TR ("Hardware Address:") <<var. hardwareaddress ();Panax Notoginseng  -         //read a list of associated information for an IP address theQlist<qnetworkaddressentry> entrylist =var. AddressEntries (); +         foreach(Qnetworkaddressentry entry, entrylist) { AQdebug () <<"IP:"<<Entry.ip (). toString (); theQdebug () <<"Subnet Mask:"<<Entry.netmask (). toString (); +Qdebug () <<"Broadcast address:"<<entry.broadcast (). toString (); -         } $     } $  - #endif - } the  -mainwindow::~MainWindow ()Wuyi { the  -}

Qt-A comprehensive example of obtaining network interface information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.