qt-obtaining the Qnetworkinterface of host network information

Source: Internet
Author: User

The Qnetworkinterface class provides a list of host IP addresses and network interfaces.

Qnetworkinterface represents a network interface that is bound to the host when the current program is running . Each network interface may contain 0 or more IP addresses, and each IP address can optionally be associated with a subnet mask and/or a broadcast address. Such a list can be obtained by Qnetworkinterface's addressentries () method. As an alternative, when the subnet mask or broadcast address is not required, you can use the alladdresses () convenience function to obtain only the IP address.

Note: The Qnetworkaddressentry class stores an IP address that is supported by the network interface, along with its associated subnet mask and broadcast address.

Qnetworkinterface also uses the hardwareaddress () method to report the hardware address of the interface.

Not all operating systems support reporting all of these features. Only IPV4 addresses are guaranteed to be listed on all platforms, especially IPV6 addresses are currently supported only for Windows XP and related editions, Linux, MacOS, and BSDs.

Related references: Qnetworkaddressentry.

member function:alladdresses () [Static]

This convenience function returns all the IP addresses found on the host. It is equivalent to calling AddressEntries () for all objects returned by allinterfaces () to get a list of Qhostaddress objects and then calling Qhostaddress on these objects : IP () method.

1#include"mainwindow.h"2#include <QHostAddress>3#include <QNetworkInterface>4#include <QNetworkAddressEntry>5#include <QDebug>6 7Mainwindow::mainwindow (Qwidget *parent)8 : Qmainwindow (parent)9 {Tenqlist<qhostaddress> list =qnetworkinterface::alladdresses (); One  A      for(inti =0; I < list.size (); i++) -     { -Qdebug () <<list.at (i); the     } -}

member function:allinterfaces () [Static]

Returns a list of all network interfaces found on the host. If the lookup fails, a list of 0 elements is returned.

1#include"mainwindow.h"2#include <QHostAddress>3#include <QNetworkInterface>4#include <QNetworkAddressEntry>5#include <QDebug>6 7Mainwindow::mainwindow (Qwidget *parent)8 : Qmainwindow (parent)9 {TenQlist<qnetworkinterface>Interface=qnetworkinterface::allinterfaces (); One  A      for(inti =0; I <Interface. Size (); i++) -     { -Qdebug () <<Interface. at (i); theQnetworkinterface item =Interface. at (i); -Qlist<qnetworkaddressentry> entrylist =item.addressentries (); -          for(intj =0; J < Entrylist.size (); J + +) -         { +Qdebug () <<"Item #"<< J <<entrylist.at (j). IP (); -         } +     } A}

member function:hardwareaddress () const

Returns the underlying hardware address for this interface. For an Ethernet interface, this will be a string representation of the MAC address, separated by a colon. Other types of hardware addresses may be used by other interface types. Implementation do not rely on this function to return a valid MAC address.

1#include"mainwindow.h"2#include <QHostAddress>3#include <QNetworkInterface>4#include <QNetworkAddressEntry>5#include <QDebug>6 7Mainwindow::mainwindow (Qwidget *parent)8 : Qmainwindow (parent)9 {TenQlist<qnetworkinterface>Interface=qnetworkinterface::allinterfaces (); One  A      for(inti =0; I <Interface. Size (); i++) -     { -Qnetworkinterface item =Interface. at (i); theQdebug () <<item.hardwareaddress (); -     } -}

Reference: "Acquiring network interface information"-mynote

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.