QT access to local IP methods, the method of domain name resolution in QT

Source: Internet
Author: User
Tags get ip

This blog link: http://blog.csdn.net/jdh99, author: jdh, reprint please specify.

Environment: Ubuntu10.04 + Qt4.7.0

Linux get local IP method, I tried two kinds of

1. Parsing with Qhostinfo class

Qhostinfo class can parse the domain name, in theory can also parse the local IP, but after trying, I found that the parsed IP is 127.0.0.1 is the local loopback address, does not meet my needs.

Code:

[CPP]View PlainCopy
  1. Get local IP
  2. Qhostinfo Info=qhostinfo::fromname (Qhostinfo::localhostname ());
  3. //qhostinfo info=qhostinfo::fromname ("www.google.com");
  4. //sys_res.local_ip = Info.addresses (). First (). ToString ();
  5. //qdebug () << sys_res.local_ip;
  6. if (info.error ()! = Qhostinfo::noerror)
  7. {
  8. Qdebug () << "Lookup failed:" << info.errorstring ();
  9. return;
  10. }
  11. For (int i = 0;i < Info.addresses (). Size (); i++)
  12. {
  13. Qdebug () << "Found address:" << info.addresses () [I].tostring () << Endl;
  14. }


2. Parsing with Qnetworkinterface

With this class can obtain the system's underlying network parameters, after practice is feasible, I refer to the article: http://qt.csdn.net/articles.aspx?pointid=489&pointid2=7

Reference Code interception:

[CPP]View PlainCopy
  1. 2. Use the Qnetworkinterface class to obtain the IP address and network interface information for this machine.
  2. The Qnetworkinterface class provides a list of IP addresses and network interface information for the host when the program is running. Each of the network interface information contains 0 or more IP addresses, and each IP address also contains its associated subnet mask and broadcast address, which are encapsulated in a Qnetworkaddressentry object. The hardware address information is also provided in the network interface information. We will remove the previously added code from the Widge.cpp constructor and add the following code.
  3. qlist<qnetworkinterface> list = Qnetworkinterface::allinterfaces ();
  4. //Get a list of all network interfaces
  5. foreach (Qnetworkinterface interface,list)
  6. { //traverse each network interface
  7. Qdebug () << "Device:" <<interface.name ();
  8. //Device name
  9. Qdebug () << "hardwareaddress:" <<interface.hardwareaddress ();
  10. //Hardware address
  11. qlist<qnetworkaddressentry> entrylist = Interface.addressentries ();
  12. //Get a list of IP address entries, each containing an IP address, a subnet mask, and a broadcast address
  13. foreach (Qnetworkaddressentry entry,entrylist)
  14. {//traversal of each IP address entry
  15. Qdebug () << "IP Address:" <<entry.ip (). toString ();
  16. //IP Address
  17. Qdebug () << "Netmask:" <<entry.netmask (). toString ();
  18. //Subnet mask
  19. Qdebug () << "Broadcast:" <<entry.broadcast (). toString ();
  20. //Broadcast address
  21. }
  22. }
[CPP]View PlainCopy
  1. In fact, if we only want to use the Qnetworkinterface class to obtain the IP address, then there is no need to be as complex as above, this class provides a convenient function alladdresses () to obtain an IP address, for example:
  2. QString address = qnetworkinterface::alladdresses (). First (). ToString ();
  3. 3. Summary.
  4. In this section we learned how to find information about native network devices. In fact, the most commonly used in the future is the method of obtaining an IP address. We can later use a function to obtain an IP address:
  5. QString Widget::getip () //Get IP address
  6. {
  7. qlist<qhostaddress> list = qnetworkinterface::alladdresses ();
  8. foreach (qhostaddress address, list)
  9. {
  10. if (address.protocol () = = Qabstractsocket::ipv4protocol)
  11. We use IPV4 address
  12. return address.tostring ();
  13. }
  14. return 0;
  15. }


Embed this code in my program:

[CPP]View PlainCopy
    1. qlist<qhostaddress> list = qnetworkinterface::alladdresses ();
    2. foreach (qhostaddress address, list)
    3. {
    4. if (address.protocol () = = Qabstractsocket::ipv4protocol)
    5. {
    6. //ipv4 Address
    7. if (address.tostring (). Contains ("127.0."))
    8. {
    9. continue;
    10. }
    11. Sys_res.local_ip = Address.tostring ();
    12. }
    13. }
    14. if (sys_res.local_ip = = "127.0.0.1")
    15. {
    16. Qdebug () << "Get local IP fail";
    17. return;
    18. }
    19. Else
    20. {
    21. Qdebug () << sys_res.local_ip;
    22. }

http://blog.csdn.net/jdh99/article/details/6679692

QT provides the class qhostinfo that can implement the domain name parsing function, this class resolves the domain name to provide two kinds of mechanism, one kind is blocking, one kind of non-blocking signal slot mechanism, the following describes the second kind of mechanism implementation method.

Examples of this class are given in the help of QT:

[CPP]View PlainCopy
  1. Qhostinfo::lookuphost ("www.kde.org", this, SLOT (Lookedup (Qhostinfo)));
  2. When the successful domain name is parsed, the Lookedup slot function is called
  3. void Mywidget::lookedup (const qhostinfo &host)
  4. {
  5. if (host.error ()! = Qhostinfo::noerror) {
  6. Qdebug () << "Lookup failed:" << host.errorstring ();
  7. return;
  8. }
  9. foreach (qhostaddress address, host.addresses ())
  10. Qdebug () << "Found address:" << address.tostring ();
  11. }

In this example, I did a Google Domain analysis test:

[CPP]View PlainCopy
  1. Qhostinfo::lookuphost ("www.google.com",This,slot (Slot_get_ip (Qhostinfo)));
  2. void Test::slot_get_ip (Qhostinfo host_info)
  3. {
  4. if (host_info.error ()! = Qhostinfo::noerror)
  5. {
  6. Qdebug () << "Lookup failed:" << host_info.errorstring ();
  7. return;
  8. }
  9. For (int i = 0;i < Host_info.addresses (). Size (); i++)
  10. {
  11. Qdebug () << "Found address:" << host_info.addresses () [I].tostring () << Endl;
  12. }
  13. }

The parse results can be seen in the debug window:

[CPP]View PlainCopy
      1. Found Address: "64.233.183.106"
      2. Found Address: "64.233.183.147"
      3. Found Address: "64.233.183.99"
      4. Found Address: "64.233.183.103"
      5. Found Address: "64.233.183.104"
      6. Found Address: "64.233.183.105"

http://blog.csdn.net/jdh99/article/details/6675670

QT access to local IP methods, the method of domain name resolution in 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.