• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統計

            最新評論

            域名,ip相互轉換(Linux,getaddrinfo, getnameinfo)--- 轉

            參考鏈接:

            http://en.wikipedia.org/wiki/Getaddrinfo

            代碼:

             

            01/* ip_to_hostname ip */
            02#include <sys/types.h>
            03#include <sys/socket.h>
            04#include <netdb.h>
            05#include <unistd.h>
            06#include <stdio.h>
            07#include <stdlib.h>
            08#include <string.h>
            09 
            10int main(int argc, char *argv[])
            11{
            12        if (argc != 2)
            13        {
            14                fprintf(stderr, "Usage: %s hostname\n", argv[0]);
            15                exit(EXIT_FAILURE);
            16        }
            17        struct addrinfo hints;
            18        struct addrinfo *result, *result_pointer;
            19        int ret;
            20        /* obtaining address matching host */
            21        memset(&hints, 0, sizeof(struct addrinfo));
            22        hints.ai_family = AF_UNSPEC;
            23        hints.ai_socktype = SOCK_STREAM;
            24        hints.ai_flags = AI_CANONNAME | AI_NUMERICHOST;
            25        hints.ai_protocol = 0;  /* any protocol */
            26 
            27//      ret = getaddrinfo(argv[1], NULL, &hints, &result);                                                                                                            
            28        ret = getaddrinfo(argv[1], NULL, &hints, &result);
            29        if (ret != 0)
            30        {
            31                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
            32                exit(EXIT_FAILURE);
            33        }
            34        /* traverse the returned list and output the ip addresses */
            35        for (result_pointer = result; result_pointer != NULL; result_pointer = result_pointer->ai_next)
            36        {
            37                char hostname[1025] = "";
            38                ret = getnameinfo(result_pointer->ai_addr, result_pointer->ai_addrlen, hostname, sizeof(hostname), NULL, 0, NI_NAMEREQD);
            39                if (ret != 0)
            40                {
            41                        fprintf(stderr, "error in getnameinfo: %s \n", gai_strerror(ret));
            42                }
            43                else
            44                {
            45                        printf("hostname: %s \n", hostname);
            46                }
            47//              printf("hostname: %s \n", result_pointer->ai_canonname);                                                                                              
            48        }
            49        freeaddrinfo(result);
            50        exit(EXIT_SUCCESS);
            51}

             

             

            01/* hostname_to_ip hostname */
            02#include <sys/types.h>
            03#include <sys/socket.h>
            04#include <netdb.h>
            05#include <unistd.h>
            06#include <stdio.h>
            07#include <stdlib.h>
            08#include <string.h>
            09 
            10int main(int argc, char *argv[])
            11{
            12        if (argc != 2)
            13        {
            14                fprintf(stderr, "Usage: %s hostname\n", argv[0]);
            15                exit(EXIT_FAILURE);
            16        }
            17        struct addrinfo hints;
            18        struct addrinfo *result, *result_pointer;
            19        int ret;
            20        /* obtaining address matching host */
            21        memset(&hints, 0, sizeof(struct addrinfo));
            22        hints.ai_family = AF_UNSPEC;
            23        hints.ai_socktype = SOCK_STREAM;
            24        hints.ai_flags = AI_CANONNAME;
            25        hints.ai_protocol = 0;  /* any protocol */
            26 
            27//      ret = getaddrinfo(argv[1], NULL, &hints, &result);                                                                                                            
            28        ret = getaddrinfo(argv[1], NULL, &hints, &result);
            29        if (ret != 0)
            30        {
            31                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
            32                exit(EXIT_FAILURE);
            33        }
            34        /* traverse the returned list and output the ip addresses */
            35        for (result_pointer = result; result_pointer != NULL; result_pointer = result_pointer->ai_next)
            36        {
            37                char hostname[1025] = "";
            38                ret = getnameinfo(result_pointer->ai_addr, result_pointer->ai_addrlen, hostname, sizeof(hostname), NULL, 0, NI_NUMERICHOST);
            39                if (ret != 0)
            40                {
            41                        fprintf(stderr, "error in getnameinfo: %s \n", gai_strerror(ret));
            42                        continue;
            43                }
            44                else
            45                {
            46                        printf("IP: %s \n", hostname);
            47                }
            48        }
            49        freeaddrinfo(result);
            50        exit(EXIT_SUCCESS);
            51}

             

            結果:

            root@localhost :/home/James/mypro/Linux-Pro/Network# ./hostname_to_ip baidu.com
            IP: 123.125.114.144
            IP: 220.181.111.85
            IP: 220.181.111.86
            root@localhost :/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 220.181.111.86
            error in getnameinfo: Name or service not known
            root@localhost :/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 10.0.0.78
            hostname: localhost

            root@localhost:/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 59.66.137.62
            hostname: th137062.ip.tsinghua.edu.cn
            root@localhost:/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 202.106.182.229
            hostname: mail182-229.sinamail.sina.com.cn
            root@localhost:/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 211.147.4.7
            hostname: mail3.douban.com

            posted on 2012-11-12 14:44 大龍 閱讀(4175) 評論(0)  編輯 收藏 引用

            久久婷婷五月综合国产尤物app| 亚洲国产成人精品91久久久| 久久国产色AV免费看| 色狠狠久久综合网| 精品一区二区久久| 色妞色综合久久夜夜| 99久久综合狠狠综合久久| 国产精品美女久久福利网站| 激情伊人五月天久久综合| 国产高潮久久免费观看| 久久久久av无码免费网| 国产无套内射久久久国产| 亚洲狠狠婷婷综合久久久久| 精品久久久久久99人妻| 日韩精品久久无码人妻中文字幕| 久久99免费视频| 青青草原精品99久久精品66| 一本色道久久88综合日韩精品| 91精品国产91久久综合| 欧美麻豆久久久久久中文| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 97久久精品午夜一区二区| 一级做a爰片久久毛片看看| 精品无码久久久久久久久久| 国产成人精品久久一区二区三区 | 丁香色欲久久久久久综合网| 久久99精品国产麻豆婷婷| 国产成人精品久久二区二区| 久久久久久九九99精品| 久久综合综合久久综合| 久久久久亚洲av无码专区喷水| 久久婷婷五月综合成人D啪| 噜噜噜色噜噜噜久久| 久久免费看黄a级毛片| 久久午夜无码鲁丝片秋霞| 久久免费看黄a级毛片| 无码精品久久久天天影视| 久久天天躁狠狠躁夜夜avapp| 色偷偷久久一区二区三区| 久久久久人妻一区精品性色av | 一级做a爰片久久毛片看看|