1, under the QT
QString runtimeconfig::ipaddress (QString network) {qlist<QNetworkAddressEntry>list; QString localipaddress; QnetworkinterfaceInterface=qnetworkinterface::interfacefromname (network); List=Interface. AddressEntries (); if(!List.isEmpty ()) {localipaddress= list.at (0). IP (). toString (); } returnlocalipaddress;} QString runtimeconfig::macaddress (QString network) {qlist<QNetworkInterface>netlist; intNetcount; intNeti; Qnetworkinterface mynet; Netlist=qnetworkinterface::allinterfaces (); Netcount=Netlist.count (); for(Neti =0; Neti < Netcount; Neti + +) { if(Netlist.at (Neti). IsValid () && netlist.at (Neti). Name () = =Network) {MyNet=netlist.at (Neti); Break; } } return(Mynet.hardwareaddress ());}
2. C language
#include <stdio.h>#include<string.h>#include<sys/types.h>#include<sys/socket.h>#include<sys/ioctl.h>#include<netinet/inch.h>#include<net/if.h>#include<net/if_arp.h>#include<arpa/inet.h>#include<errno.h>#include"getinfo.h"intGetIP (Char*ip,Char*name) { intsock; structsockaddr_in sin; structIfreq IFR; Sock= Socket (Af_inet, SOCK_DGRAM,0); if(Sock = =-1) {perror ("Socket"); return-1; } strncpy (Ifr.ifr_name, name, Ifnamsiz); Ifr.ifr_name[ifnamsiz-1] =0; if(IOCTL (sock, SIOCGIFADDR, &IFR) <0) {perror ("IOCTL"); return-1; } memcpy (&sin, &IFR.IFR_ADDR,sizeof(sin)); sprintf (IP,"%s", Inet_ntoa (SIN.SIN_ADDR)); return 0;}intGetmac (Char*mac,Char*name) { structIfreq Ifreq; intsock; if(Sock=socket (Af_inet,sock_stream,0)) <0) {perror ("Socket"); return 2; } strcpy (Ifreq.ifr_name, name); if(IOCTL (SOCK,SIOCGIFHWADDR,&IFREQ) <0) {perror ("IOCTL"); return 3; } sprintf (Mac,"%02x:%02x:%02x:%02x:%02x:%02x", (unsignedChar) ifreq.ifr_hwaddr.sa_data[0], (unsignedChar) ifreq.ifr_hwaddr.sa_data[1], (unsignedChar) ifreq.ifr_hwaddr.sa_data[2], (unsignedChar) ifreq.ifr_hwaddr.sa_data[3], (unsignedChar) ifreq.ifr_hwaddr.sa_data[4], (unsignedChar) ifreq.ifr_hwaddr.sa_data[5]); return 0;}
Linux QT and C language through the network card name to obtain the network card IP and Mac