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
- Get local IP
- Qhostinfo Info=qhostinfo::fromname (Qhostinfo::localhostname ());
- //qhostinfo info=qhostinfo::fromname ("www.google.com");
- //sys_res.local_ip = Info.addresses (). First (). ToString ();
- //qdebug () << sys_res.local_ip;
- if (info.error ()! = Qhostinfo::noerror)
- {
- Qdebug () << "Lookup failed:" << info.errorstring ();
- return;
- }
- For (int i = 0;i < Info.addresses (). Size (); i++)
- {
- Qdebug () << "Found address:" << info.addresses () [I].tostring () << Endl;
- }
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
- 2. Use the Qnetworkinterface class to obtain the IP address and network interface information for this machine.
- 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.
- qlist<qnetworkinterface> list = Qnetworkinterface::allinterfaces ();
- //Get a list of all network interfaces
- foreach (Qnetworkinterface interface,list)
- { //traverse each network interface
- Qdebug () << "Device:" <<interface.name ();
- //Device name
- Qdebug () << "hardwareaddress:" <<interface.hardwareaddress ();
- //Hardware address
- qlist<qnetworkaddressentry> entrylist = Interface.addressentries ();
- //Get a list of IP address entries, each containing an IP address, a subnet mask, and a broadcast address
- foreach (Qnetworkaddressentry entry,entrylist)
- {//traversal of each IP address entry
- Qdebug () << "IP Address:" <<entry.ip (). toString ();
- //IP Address
- Qdebug () << "Netmask:" <<entry.netmask (). toString ();
- //Subnet mask
- Qdebug () << "Broadcast:" <<entry.broadcast (). toString ();
- //Broadcast address
- }
- }
[CPP]View PlainCopy
- 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:
- QString address = qnetworkinterface::alladdresses (). First (). ToString ();
- 3. Summary.
- 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:
- QString Widget::getip () //Get IP address
- {
- qlist<qhostaddress> list = qnetworkinterface::alladdresses ();
- foreach (qhostaddress address, list)
- {
- if (address.protocol () = = Qabstractsocket::ipv4protocol)
- We use IPV4 address
- return address.tostring ();
- }
- return 0;
- }
Embed this code in my program:
[CPP]View PlainCopy
- qlist<qhostaddress> list = qnetworkinterface::alladdresses ();
- foreach (qhostaddress address, list)
- {
- if (address.protocol () = = Qabstractsocket::ipv4protocol)
- {
- //ipv4 Address
- if (address.tostring (). Contains ("127.0."))
- {
- continue;
- }
- Sys_res.local_ip = Address.tostring ();
- }
- }
- if (sys_res.local_ip = = "127.0.0.1")
- {
- Qdebug () << "Get local IP fail";
- return;
- }
- Else
- {
- Qdebug () << sys_res.local_ip;
- }
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
- Qhostinfo::lookuphost ("www.kde.org", this, SLOT (Lookedup (Qhostinfo)));
- When the successful domain name is parsed, the Lookedup slot function is called
- void Mywidget::lookedup (const qhostinfo &host)
- {
- if (host.error ()! = Qhostinfo::noerror) {
- Qdebug () << "Lookup failed:" << host.errorstring ();
- return;
- }
- foreach (qhostaddress address, host.addresses ())
- Qdebug () << "Found address:" << address.tostring ();
- }
In this example, I did a Google Domain analysis test:
[CPP]View PlainCopy
- Qhostinfo::lookuphost ("www.google.com",This,slot (Slot_get_ip (Qhostinfo)));
- void Test::slot_get_ip (Qhostinfo host_info)
- {
- if (host_info.error ()! = Qhostinfo::noerror)
- {
- Qdebug () << "Lookup failed:" << host_info.errorstring ();
- return;
- }
- For (int i = 0;i < Host_info.addresses (). Size (); i++)
- {
- Qdebug () << "Found address:" << host_info.addresses () [I].tostring () << Endl;
- }
- }
The parse results can be seen in the debug window:
[CPP]View PlainCopy
- Found Address: "64.233.183.106"
- Found Address: "64.233.183.147"
- Found Address: "64.233.183.99"
- Found Address: "64.233.183.103"
- Found Address: "64.233.183.104"
- 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