iOS 使用gethostbyname() 解析域名得到IP

解析域名得到IP

參考:http://www.cocoachina.com/bbs/read.php?tid-142713-page-1.html

#include <netdb.h> 
#include <arpa/inet.h>
 
 
-(NSString *) getIPWithHostName:(const NSString *)hostName 
{ 
    const char *hostN= [hostName UTF8String]; 
    struct hostent* phot; 
     
    @try { 
        phot = gethostbyname(hostN); 
         
    } 
    @catch (NSException *exception) { 
        return nil; 
    } 
     
    struct in_addr ip_addr; 
    memcpy(&ip_addr, phot->h_addr_list[0], 4); 
    char ip[20] = {0}; 
    inet_ntop(AF_INET, &ip_addr, ip, sizeof(ip)); 
     
    NSString* strIPAddress = [NSString stringWithUTF8String:ip]; 
    return strIPAddress; 
}

判斷一個字符串是IP地址還是域名地址

來自:http://wolfbt.blog.163.com/blog/static/176771053200985102454688/

inet_addr(addr) != INADDR_NONE // is ip 
gethostbyname(addr) != 0 // is host

   if (inet_addr(ip)== INADDR_NONE)
   {
    struct hostent *hp = NULL;
    if ((hp = gethostbyname(ip)) != NULL)
    {
     in_addr in;
     memcpy(&in, hp->h_addr, hp->h_length);
     strcpy(tgtIP,inet_ntoa(in));
    }
   }
   else
    strcpy(tgtIP,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);

 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




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章