gethostbyaddr由IP地址取得网络数据

 转载请保留: http://www.cnscn.org( CNS电脑与英语学习网)
相关函数:gethostbyname
表头文件:#inlcude <netdb.h>
函数定义:struct  hostent  *gethostbyaddr(const char *addr, int len, int type)
函数说明: gethostbyaddr()会返回一个hostent结构,参数addr可以为IPv4或IPv6的IP地址,参数len为参数addr的长度,参数type为AF_INET。结构hostent定义请参gethostbyname()
返回值   :成功则返回hostent结构指针, 失败则返回NULL指针,错误原因存于h_errno变量
错误代码:HOST_NOT_FOUND    找不到指定的主机
               NO_ADDRESS             该主机有名称却无IP地址
               NO_RECOVERY           域名服务器有错误发生
               TRY_AGAIN               请再调用一次

范例:
#include <netdb.h>
#include <sys/socket.h>

main(int argc, char *argv[])
{
    struct hostent  *host;
    if(argc<2) return;
    host = gethostbyaddr(argv[1], sizeof(argv[1]), AF_INET);
    if(host == (struct hostent * ) NULL)
         herror("gethostbyaddr");
    else{
           printf("name :%s/n",  host->h_name);
           printf("type  :%s/n", host->h_addrtype);
           printf("addr  :%s/n", host->h_addr_list[0]);
    }
         
}

你可能感兴趣的:(网络,list,struct,服务器,null)