获得本机IP:

类别:编程语言 点击:0 评论:0 推荐:

获得本机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