#include <netdb.h>
    #include <sys/socket.h>

struct hostent *gethostbyname(const char *name);
    这个函数的传入值是域名或者主机名,例如"www.google.cn"等等。传出值,是一个hostent的结构。如果函数调用失败,将返回NULL。

struct hostent
    {
        char    *h_name;                
        char    **h_aliases; 
        int     h_addrtype;
        int     h_length;
        char    **h_addr_list; 
        #define h_addr h_addr_list[0] 
    };

    hostent->h_name
    表示的是主机的规范名。例如www.google.com的规范名其实是www.l.google.com。
    hostent->h_aliases
    表示的是主机的别名.www.google.com就是google他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。
    hostent->h_addrtype     
    表示的是主机ip地址的类型,到底是ipv4(AF_INET),还是pv6(AF_INET6)
    hostent->h_length       
    表示的是主机ip地址的长度
    hostent->h_addr_lisst 
    表示的是主机的ip地址,注意,这个是以网络字节序存储的。千万不要直接用printf带%s参数来打这个东西,会有问题的哇。所以到真正需要打印出这个IP的话,需要调用inet_ntop()。

const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) :
    这个函数,是将类型为af的网络地址结构src,转换成主机序的字符串形式,存放在长度为cnt的字符串中。返回指向dst的一个指针。如果函数调用错误,返回值是NULL。

#include <netdb.h>
#include <sys/socket.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    char   *ptr, **pptr;
    struct hostent *hptr;
    char   str[32];
    ptr = argv[1];

if((hptr = gethostbyname(ptr)) == NULL)
    {
        printf(" gethostbyname error for host:%s\n", ptr);
        return 0; 
    }

printf("official hostname:%s\n",hptr->h_name);
    for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)
        printf(" alias:%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)));
            printf(" first address: %s\n", 
                       inet_ntop(hptr->h_addrtype, hptr->h_addr, str, sizeof(str)));
        break;
        default:
            printf("unknown address type\n");
        break;
    }

return 0;
}

编译运行
-----------------------------
# gcc test.c
# ./a.out www.baidu.com
official hostname:www.a.shifen.com
alias:www.baidu.com
address:121.14.88.11
address:121.14.89.11
first address: 121.14.88.11

gethostbyname() -- 用域名或主机名获取IP地址的更多相关文章

  1. (转)gethostbyname() -- 用域名或主机名获取IP地址

    struct hostent *gethostbyname(const char *name); 这个函数的传入值是域名或者主机名,例如"www.google.cn"等等.传出值, ...

  2. gethostname(获取主机名)、gethostbyname(由主机名获取IP地址)

    int gethostname(char *name, size_t len);获取本地主机名存入name[len],成功返回0,失败返回-1: struct hostent * gethostbyn ...

  3. java 获取局域网中的全部主机名和IP地址

    DOS命令 命令 意义 net view 获取局域网中的全部主机名 ipconfig -all 获取本地IP,主机名,MAC地址 arp -a 获取本局域网中的全部IP地址和物理地址 ping -a ...

  4. Linux学习笔记(10)linux网络管理与配置之一——主机名与IP地址,DNS解析与本地hosts解析(1-4)

    Linux学习笔记(10)linux网络管理与配置之一——主机名与IP地址,DNS解析与本地hosts解析 大纲目录 0.常用linux基础网络命令 1.配置主机名 2.配置网卡信息与IP地址 3.配 ...

  5. Solaris 10主机名和IP地址步骤

    1.修改主机名: hostname newname vi /etc/hosts vi /etc/hostname.e1000g0 vi /etc/nodename init 6 #重启 --confi ...

  6. 二十二、utl_inaddr(用于取得局域网或Internet环境中的主机名和IP地址)

    1.概述 作用:用于取得局域网或Internet环境中的主机名和IP地址. 2.包的组成 1).get_host_name作用:用于取得指定IP地址所对应的主机名语法:utl_inaddr.get_h ...

  7. Asp.net MVC获取访问系统的客户端计算机的主机名和IP地址

    string HostName = string.Empty; string ip = string.Empty; string ipv4 = String.Empty; if (!string.Is ...

  8. Linux CentOS7.0 (02)修改主机名和ip地址

    一.主机名修改 1.查看命令 在CentOS中,有三种定义的主机名:静态的(static),瞬态的(transient),和灵活的(pretty). "静态"主机名也称为内核主机名 ...

  9. 修改Linux的基本配置(修改主机名修改ip地址安装JDK/Tomcat/MySQL等等)

    (一)基本操作修改 修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=server1.itcast.cn 修改ip地址 vi /etc/s ...

随机推荐

  1. 【转】利用Ajax.BeginForm提交文件

    Ajax.BeginForm @using (Ajax.BeginForm("YourAction", "YourController", new AjaxOp ...

  2. 【转】Ubuntu 上编译Android出现cannot find -lstdc++解决办法

    [转]Ubuntu 上编译Android出现cannot find -lstdc++解决办法 在Ubuntu 12.04 x86_64机器上编译Android出现下面错误,是因为找不到32bit的li ...

  3. Session、SessionId和Cookie的关系

    Session是保存在服务器中的,SessionId是保存在Cookie中的. 当用户·登录时候,系统会将"用户名"和"密码"保存到Session中,系统会给每 ...

  4. python命令行解析工具argparse模块【4】

            上一节我们讲解了add_argument()方法,这一节我们将学习parse_args()方法.          parse_args()方法的作用是解析命令行参数,并返回解析之后的 ...

  5. js方法中的this

    比如有个function: function ServiceMy(services) { //存放this,用于调试用 var tmp_this = this; this.services = []; ...

  6. Linux学习之查找命令find

    1.find  -name 根据名称查找文件 *通配所有字符: ?统配单个字符 2.find -iname 根据名称不区分大小写 3.find -size  根据文件大小 find / -size + ...

  7. (Problem 35)Circular primes

    The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, ...

  8. 快捷查看dll的PublicKeyToken

    @echo off d: cd D:\Win2003\Microsoft Visual Studio 10.0\VC\ call vcvarsall.bat x86 echo. if not '%1' ...

  9. SQL分页存储过程(不支持多表联合查询,不支持多字段排序)

    CREATE PROCEDURE [dbo].[Pro_GetPageOfRecords] @PageSize INT=20, --分页大小 @CurrentPage INT, --第几页 @Clum ...

  10. Datamatrix码

    DataMatrix二维条码原名Datacode,由美国国际资料公司(International Data Matrix, 简称ID Matrix)於1989年发明.DataMatrix二维条码是一种 ...