`

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

阅读更多

 

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


    #include <netdb.h>

    #include <sys/socket.h>

    #include <unistd.h>  

    #include <sys/types.h>

    #include <netdb.h>

    #include <netinet/in.h>  

    #include <stdlib.h> 

    #include <netinet/in.h>   

    #include <arpa/inet.h> 

    #include <stdio.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

分享到:
评论
1 楼 stecdeng 2014-05-06  
有验证么  百度不是获取到的这个IP

相关推荐

    VC gethostbyname获取主机名和IP地址.rar

    VC 获取主机名和IP地址,也就是根据主机名获得IP地址,使用了gethostbyname函数,然后再使用addr.Format格式化获取到IP地址。具体来看以下代码获取到IP地址:  // 获得主机ip地址  struct hostent * phost;  ...

    Python3获取电脑IP、主机名、Mac地址的方法示例

    本文实例讲述了Python3获取电脑IP、主机名、Mac地址的方法。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- #! python3 ''' Created on 2019年4月11日 @author: Administrator ''' import socket import...

    使用GetHostByName获取IP

    在C++Builder开发环境下,使用GetHostByName获取域名或者网站的IP地址

    IP地址与主机名相互转换解析

    利用gethostbyname和gethostbyaddr进行IP与DNS间解析。

    php gethostbyname获取域名ip地址函数详解

    php gethostbyname获取域名ip地址函数,需要根据域名得到ip地址的朋友有福了。

    IP地址_viasualbasic_VBSocketIP_vb更改主机名_

    VB Socket获取主机名和IP地址,获取主机名是使用了VB中的GetComputerName直接得到,然后对Socket初始化,使用gethostbyname取得主机地址,再获取到IP后进行转化为标准的IP格式。转换过程如下: RtlMoveMemory host ...

    C++ ethostbyname 通过域名得到IP地址 通过网址得到IP地址.txt

    c++ gethostbyname 通过域名得到IP地址

    MFC实现通过计算机名获取IP地址实例

    MFC实现通过计算机名获取IP地址实例,主要使用了一个gethostbyname()函数。供大学学习交流。

    用gethostbyname来获得域名信息

    用gethostbyname来获得域名信息

    获取ip源程序

    用c++如何获取本机ip? 其实这是一个很简单的程序,不多说,看代码 //------主程序--------------------------------------------------------------------- #include #pragma hdrstop #include "WINSOCK2.H" #...

    获取IP地址和计算机名的程序设计

    题 目: 获取IP地址和计算机名的程序设计 初始条件: Windows 2000 / 2003 / XP ; Visual C++ 要求完成的主要任务: (包括课程设计工作量及其技术要求,以及说明书撰写等具体要求) 1. 熟悉相关函数gethostname, ...

    gethostbyname 用法

    gethostbyname 的用法,查询域名的IP,很简单,免费下载

    python shell根据ip获取主机名代码示例

    这篇文章里我们主要分享了python中shell 根据 ip 获取 hostname 或根据 hostname 获取 ip的代码,具体介绍如下。 笔者有时候需要根据hostname获取ip 比如根据machine.company.com 获得ip 10.173.14.117 方法1:利用...

    c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址 // GetHostIP.cpp : 定义控制台应用程序的入口点。 // #include stdafx.h #include #include #include #include #pragma comment(lib, ws2_32.lib) int main(int argc, char **argv)...

    delphi网络应用

    1.GetIP(获取IP地址程序)-GetHostByName(使用GetHostByName函数获取IP程序)-WSAAsyncGetHostByName(使用WSAAsyncGetHostByName函数获取IP地址程序)-MultiIP(多IP情况的处理程序)-ReadIP and IP(关于IP地址...

    Windsock获取本机IP

    利用gethostname and gethostbyname 获取本地Ip

    Python将主机名转换为IP地址的方法

    关于主机名转IP地址只记住两点即可: 1、英特网协议一直都是用4字节的IP将包转发给目的地; 2、至于主机名是如何转换成IP地址的,这是操作系统关心的事情; import socket hostname = 'www.baidu.com' addr = ...

    python实现批量将域名解析成ip

    批量将域名转成ip,为了避免误差,该工具同时使用dig工具和python自带的gethostbyname_ex工具解析域名,并且最大化的收集所有ip。 如果使用windows需要安装dig工具(mac或者linux忽略)。 安装教程:...

Global site tag (gtag.js) - Google Analytics