积累点滴,一种获取板子网卡Ip方法
1 int main() 2 { 3 struct ifreq stIfconfig; 4 5 char szIPeth0[16]; 6 char szIPeth1[16]; 7 memset(szIPeth0,0x0,16); 8 memset(szIPeth1,0x0,16); 9 10 int nNetWorkSocket = socket(AF_INET, SOCK_DGRAM, 0); 11 12 //获取eth0的ip 13 strncpy(stIfconfig.ifr_name, "eth0",5); 14 if (ioctl(nNetWorkSocket, SIOCGIFADDR, &stIfconfig) < 0) 15 { 16 printf("ioctl does not get eth0!") 17 } 18 else 19 { 20 strncpy(szIPeth0,inet_ntoa(((struct sockaddr_in*)&(stIfconfig.ifr_addr))->sin_addr),16); 21 } 22 23 //获取eth1的ip 24 strncpy(stIfconfig.ifr_name, "eth1",5); 25 if (ioctl(nNetWorkSocket, SIOCGIFADDR, &stIfconfig) < 0) 26 { 27 printf("ioctl does not get eth1!") 28 } 29 else 30 { 31 strncpy(szIPeth1,inet_ntoa(((struct sockaddr_in*)&(stIfconfig.ifr_addr))->sin_addr),16); 32 } 33 34 printf("eth0 = %s", szIPeth0); 35 printf("eth1 = %s", szIPeth1); 36 37 }