QT Learning Path ——— get native network information

Source: Internet
Author: User
Lead

With HTTP and FTP in front, it's time to talk about UDP and TCP.        However, before explaining them, let's explain in this section a noun that will be used frequently, that is, the IP address. For the IP address, in fact, people who will be online should have heard of it. If you are really not a property, then simply say: IP is internetprotocol (the interconnection between the network protocol), the Protocol is the rule, the Earth people use the same rules, so we can access any Web site, and the IP address is the Internet when you assigned to your machine an address. If the network is likened to a map, the IP address is like the latitude and longitude on the map, which determines the location of your host on the network. Actually know that we will use the IP address to represent a computer in the network is enough. (^_^ is not necessarily scientific but straightforward to describe)
Here's how to get your computer's IP address and other network information. In this section, we will refer to several classes such as Qhostinfo,qhostaddress,qnetworkinterface and Qnetworkaddressentry in the network module (qtnetworkmodule). Here is the details.


Environment: Windows Xp + Qt 4.8.5+qt Creator2.8.0



Directory
First, using Qhostinfo to obtain the hostname and IP address two, through the Qnetworkinterface class to obtain the local IP address and network interface information



Body
First, use Qhostinfo to get the hostname and IP address
We create a new Qt Gui application with the project name MyIP, the base class select Qwidget, and the class name keeps the Widget intact. When you are finished, open the Myip.pro file, add a line of code: QT + = network, and then save the file. The following open widget.h file add header file contains: #include <QtNetwork>
(1) Gets the host name. We add code in 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 get the computer name of the machine. To run the program, the information in the following application output column is as follows:



As you can see, the computer name is obtained here. We can right-click on the "My Computer" icon on the desktop and select the "Properties" menu to see the "Computer name" item, and the output is the same as shown here.




(2) Get the IP address of the machine. We continue to add code in the constructor: Qhostinfo info = qhostinfo:: FromName (Localhostname); Qdebug () << "IP Address:" <<info.addresses (); Call the FromName () function of the Qhostinfo class and use the host name parameter obtained above to get the information for this machine. The addresses () function of the Qhostinfo class is then used to obtain all IP address information for this machine. To run the program, the output information is as follows:





I have only one IP address here. However, on other systems, there may be multiple IP addresses, which may contain IPv4 and IPv6 addresses, generally we need to use the IPV4 address, so we can only output IPV4 address. We continue to add code: foreach (Qhostaddress address,info.addresses ()) {if (address.protocol () = = Qabstractsocket::ipv4proto COL) Qdebug () << address.tostring (); }
Because the IP address is managed by the Qhostaddress class, we can use the class to get an IP address, and then use the class's protocol () function to determine if it is an IPv4 address. If it is a IPV6 address, you can use Qabstractsocket::ipv6protocol to determine.       Finally, we output the IP address in the qstring type. The IP address we will use in the future is obtained using this method, so this must be mastered. The results are as follows:




(3) Obtain an IP address by hostname. The above describes the computer name of the machine to get the IP address of the machine. In fact, the Qhostinfo class can also be used to obtain an IP address for any host name, such as the IP address of a Web site. Here we can use the Lookuphost () function. It is based on the signal and slot, and once the IP address is found, the slot function is triggered. The specific usage is as follows. We add a private slot function in the widget.h file: Private slots:void lookedup (const qhostinfo &host); The code added above is then commented out in the constructor in Widget.cpp (you can annotate the code by selecting all the code and then pressing the ctrl+/shortcut key), and then adding the following code: Qhostinfo:: Lookuphost ("www.qter.org" , this, SLOT (Lookedup (Qhostinfo)));
Here we query the Qter open source community IP address, if found, will execute our lookedup () function. Add the implementation code for the Lookedup () function in Widget.cpp:
void   Widget:: lookedup (const   QHOSTINFO   &host) {     qdebug ()   <<

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.