QT Get NIC Information qnetworkinterface

Source: Internet
Author: User

The

Qnetworkinterface is a class that manages network connections, using which he can enumerate the local network connections and get their properties. More commonly used functions are: qlist<qnetworkaddressentry> qnetworkinterface::addressentries ()   Get address list, apply as follows:?


//prototype qlist<qnetworkaddressentry> qnetworkinterface::addressent Ries () const//For example, to obtain all local IP and netmask//allinterfaces () is a static function, which is used to obtain this machine so connected. Qlist<qnetworkinterface> networkinterface = Qnetworkinterface::allinterfaces (); for  (Qlist<qnetworkinterface>::const_iterator i = Networkinterface.const_begin (); I!= Networkinterface.const_end (); ++i {  //Get connection address list    qlist<qnetworkaddressentry> addressentrieslist = (*i). AddressEntries ();    for  (Qlist<qnetworkaddressentry>::const_iterator j = Addressentrieslist.constbegin (); J!= Addressentrieslist.constend (); ++J) {    /output IP      qdebug () << (*i). IP (). toString ();   & nbsp; //Output netmask      qdebug () << (*i). IP (). toString ();   }
QString qnetworkinterface::hardwareaddress () gets the NIC MAC address. For example, in the previous example:
1 (*i). Hardwareaddress ();
QString Qnetworkinterface::name () Gets the network card name. This name is the system assigned, is a string, in the Windows platform, you can pass this string as a parameter to the API, to get or set other Qt can not get or set properties, such as Gateway, DNS, network card type, WINS server access and settings, you can refer to Windows Getadapterinfo and Getperadaterinfo in the API to implement these functions. Qnetworkinterface Qnetworkinterface::interfaceformname (), by name to get a Qnetworkinterface object or pointer, where the name is also the name referred to above. QString qnetworkinterface::humanreadablename (), get the network connection name, where the name generally refers to "local connection", "eth0" and "Lo" and so on. int Qnetworkinterface::index (), network card index, which is the index that the system assigns to the network card at startup, is an integer value, and of course it can be the interface between Qt and system API, just like name (). Qnetworkinterface qnetworkinterface::interfacefromindex (int index), which retrieves an Qnetworkinterface object or pointer by index value. The index value here is the above-indicated integer value. Qlist<qhostaddress> qnetworkinterface::alladdresses (), Gets the address list, where only the IP address list is returned, so if you want to get an IP address only, you can use this function, for example:?
Allinterfaces () is a static function that is used to obtain the local connection. Qlist<qnetworkinterface> networkinterface = Qnetworkinterface::allinterfaces (); for (Qlist<qnetworkinterface>::const_iterator i = Networkinterface.const_begin (); I!= networkInterface.const_ End ();    ++i) {//Get IP Address list qlist<qhostaddress> addresseslist = (*i). alladdresses ();      for (Qlist<qhostaddress>::const_iterator j = Addresseslist.constbegin (); J!= Addresseslist.constend (); ++j) {    Output IP qdebug () << (*i). toString (); } }


QT Gets the network IP address class qhhostinfo,qnetworkinterface,qhostadress

Recently in learning QT network programming, based on TCP and UDP protocols.

Read some other people's programs and QT4 examples, the biggest problem that bothers me is to get the IP class, summed up a lot of.

This paper mainly introduces the Qhostinfo,qhostaddress,qnetworkinterface and Qnetaddressentry in common Qtnetwork module.

1. Qhostinfo class gets host name and IP address

(1) Get host Name: Qhostinfo::localhostname ()

QString Localhostname=qhostinfo::localhostname ();

(2) Obtain the IP address: qhostinfo info=qhostinfo::fromname (Localhostname);//To obtain information about the machine based on the host name obtained above

Info.addresses ();//qhostinfo address function to obtain native IP addresses

If there are more than one IP address IPv4 and IPv6:

foreach (Qhostaddress address,info.addresses ())

{

if (Address.protocol () ==qabstractsocket::ipv4p)//Only the address of the IPv4 protocol

Qdebug () <<address.tostring ();

}

If it is an IPv6 address, you can use Qabstractsocket::ipv6protocol to implement it.

The Qhostaddress class is a class that manages IP addresses, and all IP is managed by this class.

(3) Get IP by host name

Qhostinfo class can get any host name IP, such as the IP address of the Web site, you can use the Lookuphost () function to obtain, he is based on signals and slots, once found that the IP address will trigger the slot function.

First, define a slot function:

void Lookedup (const qhostinfo &host)

{

Qdebug () <

}

Qhostinfo::lookuphost ("www.baidu.com",

This,slot (Lookedup (Qhostinfo)));

Through the query Baidu URL IP address, if found, will perform lookedup () function.

(4) Get host name through IP address

Call the Lookuphost () function to reverse the lookup of the host name by entering an IP address. Replace the function above.

2. Qnetworkinterface class gets the IP address and network interface information of the program running this machine

The Qnetworkinterface class provides a list of host IP addresses and network interface information that the program runs on. In a network interface information, contains one or more IP addresses, and each IP address contains the subnet mask and broadcast address associated with it. They are not encapsulated in an object qnetworkaddressentry, the network interface information also contains the hardware address information.

Qlist<qnetworkinterface>list=qnetworkinterface;:allinterfaces ()//Get all network interface information

foreach (Qnetworkinterface interface,list)

{

facilitate each interface information

qdebug<< "Device:" <<interface.name ();//Device name

qdebug<< "hardwareaddress:" <<interface.hardwareadderss ()//Get hardware Address

Qlist<qnetworkaddressentry>entrylist=interface.addressentries ()//Get IP address and subnet mask and broadcast address

foreach (Qnetworkaddressentry entry,entrylist)

{//Convenient IP Entry list

qdebug<< "IP Address:" <<entry.ip (). toString ();//Get IP

qdebug<< "Netmask:" <<entry.netmask (). toString ();//Get Subnet mask

qdebug<< "Broadcast:" <<entry.broadcast (). toString ();//Get broadcast address

}

}

If you only want to obtain an IP address, you can call the Qnetworkinterface class's alladdresses () to obtain an IP address, such as:

QString address=qnetworkinterface::alladdresses (). A (). (). toString ();

Get all IP addresses:

Qlist<qhostaddress>list=qnetworkinteface::alladdresses ();

{

foreach (Qhostaddress address,list)

{

if (Address.protocol () ==qabstractsocket::ipv4protocol)

Qdebug<<address.tostring ();

}

}

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.