Obtain Local Network Information

Source: Internet
Author: User

For IP addresses, people who access the Internet should have heard of them. If you really don't know about it, let's simply say: IP is the Internet Protocol (the Protocol for interconnection between networks). Protocol is the rule, and people on earth use the same rule, therefore, we can access any website in the world, and the IP address is the address assigned to you when you are online. If you compare a network to a map, the IP address is the same as the longitude and latitude of the map. It determines the location of your host on the network. In fact, it is enough to know that we will use IP addresses to represent a computer on the network. (^_^ Is not necessarily a scientific but straightforward expression)

The following describes how to obtain the IP address of your computer and other network information. This section involves several classes in the Network Module (qtnetwork module), such as qhostinfo, qhostaddress, qnetworkinterface, and qnetworkaddressentry. The following is a detailed description.

Create a qt4 GUI application project named myip, select the qtnetwork module, and select qwidget as the base class.

We include the header file in the widget. h file: # include <qtnetwork>

1. Use qhostinfo to obtain the Host Name and IP address.

(1) obtain the host name.

Add the code to the constructor In the widget. cpp file:

QString localHostName = QHostInfo::localHostName();

Qdebug () <"localhostname:" <localhostname;

Here we use the localhostname class of the qhostinfo class to obtain the computer name of the local machine.

 

(2) obtain the IP address of the local machine.

We continue to add code in the constructor:

QHostInfo info = QHostInfo::fromName(localHostName);    qDebug() << "localHostName: " << localHostName << endl             << "IP Address: " << info.addresses();

We apply the fromname () function of the qhostinfo class and use the host name obtained above as a parameter to obtain information about the local machine. Then, use the addresses () function of the qhostinfo class to obtain all the IP addresses of the local machine.

I only have one IP address. However, in other systems, there may be multiple IP addresses, which may contain IPv4 and IPv6 addresses. Generally, we need to use IPv4 addresses, so we can only output IPv4 addresses.

We will continue to add the Code:

foreach (QHostAddress address, info.addresses())    {        if(address.protocol() == QAbstractSocket::IPv4Protocol)            qDebug() << address.toString();    }

 

Because the IP address is managed by the qhostaddress class, we can use this class to obtain an IP address, and then use the Protocol () function of this class to determine whether it is an IPv4 address. For IPv6 addresses, you can use qiniactsocket: ipv6protocol to determine. Finally, we output the IP address in qstring type.

The IP addresses we will use in the future are obtained using this method, so we must master this.

3) obtain the IP address from the host name.

We described how to use the computer name of the Local Machine to obtain the IP address of the local machine. In fact, the qhostinfo class can also be used to obtain the IP address of any host name, such as the IP address of a website. Here we can use the lookuphost () function. It is based on signals and slots. Once an IP address is found, the slot function is triggered. The usage is as follows.

We add a private slot function in mainwindow. h file:

private slots:    void lookedUp(const QHostInfo &host);

Then, in the constructor in mainwindow. cpp, delete all the code added above, and then add the following code:

QHostInfo::lookupHost("www.baidu.com", this, SLOT(lookedUp(QHostInfo)));  
 

Here we query the IP address of the Baidu website. If we find the IP address, we will execute our lookedup () function.

Add the implementation code of the lookedup () function to mainwindow. cpp:

Void mainwindow: lookedup (const qhostinfo & host) {If (host. Error ()! = Qhostinfo: noerror) {qdebug () <"lookup failed:" 

 

In fact, we can also use the lookuphost () function to reverse query the host name by entering the IP address. You only need to replace "www.baidu.com" in the above Code with an IP address, if you are interested, you can study it, but the returned results may not be as you think.

Summary: You can see the function of the qhostinfo class: Find the IP address through the host name, or reverse query the host name through the IP address.

 

 

2. Obtain the local IP address and network interface information through the qnetworkinterface class.

The qnetworkinterface class provides a list of Host IP addresses and network interface information when the program is running. Each network interface information contains 0 or more IP addresses, and each IP Address Contains the subnet mask and broadcast address related to it, the three are encapsulated in a qnetworkaddressentry object. Hardware address information is also provided in network interface information. Delete the Code previously added to the widge. cpp constructor, and then add the following code.

// Obtain the list of all network interfaces qlist <qnetworkinterface> List = qnetworkinterface: allinterfaces (); // traverse each network interface foreach (qnetworkinterface interface, list) {// Interface Name qdebug () <"name:" <interface. name (); // hardware address qdebug () <"hardwareaddress:" <interface. hardwareaddress (); // obtain the list of IP address entries. Each entry contains an IP address, a subnet mask, and a broadcast address qlist <qnetworkaddressentry> entrylist = interface. addressentries (); // traverse each IP address entry foreach (qnetworkaddressentry entry, entrylist) {// ip address qdebug () <"IP Address:" <entry. IP (). tostring (); // subnet mask qdebug () <"netmask:" <entry. netmask (). tostring (); // broadcast address qdebug () <"broadcast:" <entry. broadcast (). tostring ();}}

 

In fact, if we only want to use the qnetworkinterface class to obtain the IP address, there is no need to be as complicated as above. This class provides a convenient function alladdresses () to obtain the IP address, for example:

Qstring address = qnetworkinterface: alladdresses (). First (). tostring ();

3. Summary.

In this section, we learned how to find information about local network devices. In fact, the most common method in the future is to obtain the IP address. In the future, we can use a function to obtain the IP Address:

Qstring Widget: getip () // obtain the IP address {qlist <qhostaddress> List = qnetworkinterface: alladdresses (); foreach (qhostaddress address, list) {If (address. protocol () = q1_actsocket: ipv4protocol) // we use the IPv4 address return address. tostring () ;}return 0 ;}

 

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.