标题:vc中将IP转换为域名
只看楼主
creep林
Rank: 1
等 级:新手上路
帖 子:14
专家分:4
注 册:2011-4-9
结帖率:66.67%
 问题点数:0 回复次数:0 
vc中将IP转换为域名
vc中将IP转换为域名
这可以用gethostbyaddr( )来完成.
和gethostbyname( )一样,它也返回一个struct hostent类型的指针.
在多线程环境下也有个替代函数gethostbyaddr_r( ).下划线后的r代表reentrant,
意味着可以安全地重入.
#include
#include
#include
#include
main(int argc, const char **argv)
{
u_long addr;
struct hostent *hp;
char **p;
if (argc != 2) {
printf("用法: %s IP地址\n", argv[0]);
exit (1);
}
if ((int)(addr = inet_addr(argv[1])) == -1) {
printf("错误的IP地址.\n");
exit (2);
}
hp = gethostbyaddr((char *)&addr, sizeof (addr), AF_INET);
if (hp == NULL) {
printf("Host information about %s not found.\n", argv[1]);
exit (3);
}
printf("Official name: %s\n",hp->h_name); //正式域名
printf("IP Addr:\n");
for (p = hp->h_addr_list; *p ; p++) //循环显示所有IP
printf(" %s\n", inet_ntoa(*p));
printf("Aliases:\n");
for (p = hp->h_aliases; *p ; p++) //循环显示所有别名
printf(" %s\n", *p);
}
搜索更多相关主题的帖子: IP地址 多线程 下划线 
2011-05-09 19:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-339324-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.131483 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved