获得本机IP:
struct hostent *h;
char host[100];
int i;
gethostname(host, 100);
if ((h = gethostbyname(host)) == NULL) {
printf("Error : %s!\n", hstrerror(h_errno));
return;
};
printf("Default IP: %s\n", inet_ntoa (*((struct in_addr *)h->h_addr)));
for (i = 0; i< h->h_length / sizeof(int); i++) {
printf("IP %d : %s\n", i+1, inet_ntoa (*((struct in_addr *)h->h_addr_list[i])));
};
本文地址:http://com.8s8s.com/it/it26395.htm