linux 通过Ip获取主机名等信息gethostbyaddr()等。
2013-03-06 10:52
555 查看
#include <stdlib.h> #include <stdio.h> #include <netdb.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int main(int argc, char **argv) { char *ptr,**pptr; struct hostent *hptr; char str[32]; char ipaddr[16]; struct in_addr *hipaddr = (struct in_addr *)malloc(sizeof(struct in_addr)); ptr = argv[1]; printf("0:%s\n",ptr); if(!inet_aton(ptr,hipaddr)) { printf("error1\n"); return 1; } if( (hptr = gethostbyaddr(hipaddr,4,AF_INET) ) == NULL) { h_errno; printf("err2 %s\n",ptr); switch(h_errno) { case HOST_NOT_FOUND:printf("111\n");break; // case NO_ADDRESS: // case NO_DATA:printf("112\n");break; case NO_RECOVERY:printf("113\n");break; case TRY_AGAIN:printf("115\n");break; } return 1; } printf("hostname:%s\n",hptr->h_name); for(pptr = hptr->h_aliases; *pptr != NULL; pptr++ ) printf("%s\n",*pptr); switch( hptr->h_addrtype) { case AF_INET: case AF_INET6: pptr = hptr->h_addr_list; for(;*pptr!=NULL;pptr++) printf("address:%s\n",inet_ntop(hptr->h_addrtype,*pptr,str,sizeof(str))); break; default: printf("default \n"); break; } return 0; } /* * //http://www.rosoo.net/a/201105/11535.html * 返回值 的gethostbyname()和gethostbyaddr()函数功能返回的 HOSTENT的结构或NULL指针,如果出现错误。错误时,h_errno的变量保存的错误号。 错误的可变h_errno的可以具有以下值: HOST_NOT_FOUND 指定的主机是未知的。 NO_ADDRESS或NO_DATA 请求的名称是有效的,但没有一个IP地址。 NO_RECOVERY 不可恢复的名称服务器发生错误。 TRY_AGAIN 一个临时错误发生在权威域名服务器。请稍后再试。 * */
相关文章推荐
- (转载)Linux网络编程-使用gethostbyaddr,通过ip地址,得到该ip地址的主机的信息
- 使用gethostbyaddr,通过ip地址,得到该ip地址的主机的信息
- 使用gethostbyaddr,通过ip地址,得到该ip地址的主机的信息
- 获取主机名、IP——gethostent(),gethostbyname(),gethostbyaddr()
- Linux 网络编程之通过IP或者主机名获取信息
- Linux LAN 通过IP获取目标主机的MAC地址
- 通过gethostbyaddr来获取计算机的名称
- 使用 python 获取 Linux 的 IP 信息(通过 ifconfig 命令)
- Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip 、获取本地主机的名字
- Linux环境下获取当前主机的IP/MAC等信息(C/C++)
- Linux下获得主机与域名-gethostbyname和gethostbyaddr
- linux通过域名获取主机信息
- Linux下获得主机与域名-gethostbyname和gethostbyaddr
- gethostbyaddr 和 gethostbyname 给定主机的信息
- Linux环境下获取当前主机的IP/MAC等信息(C/C++)
- gethostbyaddr函数获取主机名
- linux通过c语言接口获取网卡信息
- Java工具类之通过IP获取对应的位置信息及网络供应商
- 使用 python 获取 Linux 系统信息(通过dmidecode命令)
- 调用Linux的busybox,通过linux命令来获取AndRoidIP