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 (); } }
|