• <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>

            大龍的博客

            常用鏈接

            統(tǒng)計(jì)

            最新評(píng)論

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

            參考鏈接:

            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}

             

            結(jié)果:

            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 大龍 閱讀(4188) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            久久精品成人影院| 久久久久亚洲AV片无码下载蜜桃 | 久久亚洲国产午夜精品理论片| 国产综合免费精品久久久| 99久久免费国产精精品| 亚洲第一极品精品无码久久| 久久国产劲爆AV内射—百度| 久久人人添人人爽添人人片牛牛| 久久久久久国产精品免费免费| 久久国产成人午夜aⅴ影院| 91久久精品无码一区二区毛片| 久久精品视频网| 久久av免费天堂小草播放| 草草久久久无码国产专区| 久久成人精品| 久久精品国产99国产精品导航| 久久无码中文字幕东京热| 人妻久久久一区二区三区| 久久99国产精一区二区三区| 久久精品国产久精国产| 精品久久久无码中文字幕天天| 青春久久| 精品久久久久久无码专区不卡| 久久99国产精品久久99| 污污内射久久一区二区欧美日韩 | 亚洲综合精品香蕉久久网97| 国产精品青草久久久久福利99| 三级韩国一区久久二区综合 | 国产精品天天影视久久综合网| 国内精品久久久久影院网站 | 中文精品久久久久人妻不卡| 久久精品成人免费看| 久久只这里是精品66| 99久久精品国内| 精品综合久久久久久98| 久久噜噜电影你懂的| 久久久无码精品亚洲日韩蜜臀浪潮 | 久久久久久毛片免费看| 日韩人妻无码精品久久久不卡 | 伊人久久免费视频| 久久精品国产免费观看|