The Qnetworkaddressentry of QT

Source: Internet
Author: User

Briefly

The Qnetworkaddressentry class is supported by a network interface that stores an IP address, subnet mask, and broadcast address.

Each network interface can contain 0 or more IP addresses, which in turn can be associated to a subnet mask and/or a broadcast address (depending on the support of the operating system).

This class represents one such group.

    • Briefly
    • Common interfaces
    • Use
    • More references

Common interfaces

Qhostaddress Broadcast () const

Returns the broadcast address associated with the IPV4 address and subnet mask.

For IPV6 addresses, the return is always empty because the broadcast concept has been discarded in order for the system to support multicasting.


Qhostaddress IP () const

Returns the IPV4 or IPV6 address that exists in a network interface.


Qhostaddress netmask () const

Returns the subnet mask associated with the IP address. A subnet mask is a representation of an IP address, such as 255.255.0.0.

For IPv6 addresses, the prefix length is converted to an address where the number of bits set to 1 equals the prefix length. The prefix length is 64 bits (the most common value) and the subnet mask is represented as a qhostaddress with an address of FFFF:FFFF:FFFF:FFFF::.


int prefixlength () const

Returns the prefix length for this IP address. The prefix length matches the number of bits set to 1 in the subnet mask. The value of the IPV4 address is between 0-32. The value of the IPV6 address is between 0-128 and is the preferred representation of the data.

If the prefix length is not determined, return 0 (that is: netmask () returns an empty Qhostaddress ()).

For example:

255.255.240.0 converted to binary: 11111111 11111111 11110000 00000000, then the prefix length is 8*2 + 4 = 20 (1 of the number).

FFFF:FFFF:FFFF:FFFF:: Converted to binary: 1111111111111111 1111111111111111 1111111111111111 1111111111111111, then the prefix length is 16*4 = 64 (1 of the number).

Use

A convenient static function, Allinterfaces (), is provided in the Qnetworkinterface class for returning all network interfaces.

Qlist<qnetworkinterface>List= Qnetworkinterface::allinterfaces ();foreach(Qnetworkinterface NetInterface,List) {qlist<qnetworkaddressentry> entrylist = netinterface.addressentries ();foreach(Qnetworkaddressentry entry, entrylist) {//traversal of each IP addressQdebug () <<"********************"; Qdebug () <<"IP Address:"<< Entry.ip (). toString ();//IP addressQdebug () <<"Netmask:"<< Entry.netmask (). toString ();//Subnet maskQdebug () <<"Broadcast:"<< entry.broadcast (). toString ();//Broadcast addressQdebug () <<"Prefix Length:"<< entry.prefixlength ();//Prefix length}}

By traversing each of the network interface Qnetworkinterface, according to its addressentries () function, we can easily obtain all the qnetworkaddressentry, and then through the IP (), netmask (), The broadcast () function obtains the corresponding IP address, subnet mask, and broadcast address.

The output is as follows:

IP Address: "Fe80::550c:ab19:fb48:1c9%15"
Netmask: "FFFF:FFFF:FFFF:FFFF::"
Broadcast: ""
Prefix length:64

IP Address: "169.254.1.201"
Netmask: ""
Broadcast: ""
Prefix Length: 1

IP Address: "FE80::d 086:8566:6065:8954%11"
Netmask: "FFFF:FFFF:FFFF:FFFF::"
Broadcast: ""
Prefix length:64

IP Address: "172.18.4.165"
Netmask: "255.255.240.0"
Broadcast: "172.18.15.255"
Prefix length:20

IP Address: "Fe80::f864:a962:7219:f98e%16"
Netmask: "FFFF:FFFF:FFFF:FFFF::"
Broadcast: ""
Prefix length:64

IP Address: "192.168.17.1"
Netmask: "255.255.255.0"
Broadcast: "192.168.17.255"
Prefix length:24

IP Address: "Fe80::8169:691f:148e:d3cb%17"
Netmask: "FFFF:FFFF:FFFF:FFFF::"
Broadcast: ""
Prefix length:64

IP Address: "192.168.178.1"
Netmask: "255.255.255.0"
Broadcast: "192.168.178.255"
Prefix length:24

IP Address: "Fe80::5996:27a3:83b5:2ae7%18"
Netmask: "FFFF:FFFF:FFFF:FFFF::"
Broadcast: ""
Prefix length:64

IP Address: "192.168.56.1"
Netmask: "255.255.255.0"
Broadcast: "192.168.56.255"
Prefix length:24

IP Address: ":: 1"
Netmask: "Ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
Broadcast: ""
Prefix length:128

IP Address: "127.0.0.1"
Netmask: ""
Broadcast: ""
Prefix Length: 1

More references
    • Network programming of QT
    • The Qhostinfo of QT
    • The qhostaddress of QT
    • The Qnetworkinterface of QT

The Qnetworkaddressentry of QT

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.