int main()
{
struct ifreq stIfconfig;
char szIPeth0[16];
memset(szIPeth0,0x0,16);
int nNetWorkSocket = socket(AF_INET, SOCK_DGRAM, 0);
//获取eth0的ip
strncpy(stIfconfig.ifr_name, "eth0",5);
if (ioctl(nNetWorkSocket, SIOCGIFADDR, &stIfconfig) < 0)
{
printf( "ioctl does not get eth0!");
}
else
{
strncpy(szIPeth0,inet_ntoa(((struct sockaddr_in*)&(stIfconfig.ifr_addr))->sin_addr),IP_ADDR_LEN_MAX);
}
printf("IP = %s", szIPeth0);
return 0;
}