设为首页 加入收藏

TOP

如何得到多穴主机的多个IP地址
2014-11-23 20:00:29 来源: 作者: 【 】 浏览:13
Tags:如何 得到 主机 多个 地址

在网络中的多穴主机可能同时拥有多个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;

};

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇利用ATL(ActiveX模板库)创建Act.. 下一篇利用MFC开发ActiveX控件

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: