如何得到多穴主机的多个IP地址

2014-11-23 20:00:29 · 作者: · 浏览: 17

在网络中的多穴主机可能同时拥有多个IP地址,特别是在使用了动态主机地址分配时也很难知道主机上的IP地址是什么。下面利用一段C程序来列举出主机上的所有IP地址。下面是具体代码:

void print_all_ip(void)

{

char szHostName[128];

const char* pszAddr;

struct hostent * pHost;

int i,j;

if( gethostname(szHostName, 128) == 0 )

{

pHost = gethostbyname(szHostName);

for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )

{/*对每一个IP地址进行处理*/

pszAddr=inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);

printf("%s ",pszAddr);/*打印*/

}

}

}

介绍Socket编程的文章已经很多,所以接下来只对相关内容进行简单的讲解, 函数gethostname将回返回给定主机名所对应的信息,在WinSock中struct hostent的定义如下:

struct hostent

{

char FAR * h_name;

char FAR * FAR * h_aliases;

short h_addrtype;

short h_length;

char FAR * FAR * h_addr_list;

};