gethostbyname, getipnodebyname, getaddrinfo, InetAddress.getByName

 1. gethostbyname(...)

     - used only for IPv4.

     - cc gethostbyname.c -lnsl

 

-:cat gethostbyname.c
     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     #include <sys/types.h>
     #include <sys/socket.h>
     #include <netinet/in.h>
     #include <arpa/inet.h>
     #include <netdb.h>

     int main(int argc, const char **argv)
     {
          in_addr_t addr;
          struct hostent *hp;
          char **p;
          if (argc != 2) {
              (void) printf("usage: %s hostname/n", argv[0]);
              exit (1);
          }

          hp = gethostbyname(argv[1]);
          if (hp == NULL) {
              (void) printf("host information for %s not found/n", argv[1]);
              exit (3);
          }

          for (p = hp->h_addr_list; *p != 0; p++) {
              struct in_addr in;
              char **q;
              (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr));
              (void) printf("%s %s/n", inet_ntoa(in), hp->h_name);
              for (q = hp->h_aliases; *q != 0; q++)
                  (void) printf(" %s", *q);
              (void) putchar('0');
          }
          exit (0);
     }

 

2. getipnodebyname(...)

    - used for both IPv4 and IPv6.

    - cc getipnodebyname.c -lsocket -lnsl

 

-:cat getipnodebyname.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

void main(int argc, const char **argv)
{
        char abuf[INET6_ADDRSTRLEN];
        int error_num;
        struct hostent *hp;
        char **p;

        if (argc != 2) {
                (void) printf("usage: %s hostname/n", argv[0]);
                exit (1);
        }

        /* argv[1] can be a pointer to a hostname or literal IP address */
        hp = getipnodebyname(argv[1], AF_INET6, AI_DEFAULT, &error_num);
        if (hp == NULL) {
                if (error_num == TRY_AGAIN) {
                        printf("%s: unknown host or invalid literal address "
                                "(try again later)/n", argv[1]);
                } else {
                        printf("%s: unknown host or invalid literal address/n",
                                argv[1]);
                }
                exit (1);
        }

        for (p = hp->h_addr_list; *p != 0; p++) {
                struct in6_addr in6;
                char **q;

                bcopy(*p, (caddr_t)&in6, hp->h_length);
                (void) printf("%s %s", inet_ntop(AF_INET6, (void *)&in6,
                        abuf, sizeof(abuf)), hp->h_name);

                for (q = hp->h_aliases; *q != 0; q++)
                        (void) printf(" %s", *q);

                (void) putchar('0');
                printf("/n");
          }
          freehostent(hp);
          exit (0);
}

 

 

3. getaddrinfo(...)

    - used for both IPv4 and IPv6

    - cc getaddrinfo.c -lsocket -lnsl

 

-:cat getaddrinfo.c

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

void main(int argc, const char **argv)
{

        struct addrinfo hints, *res, *res1;
        int err;

        memset((void*)&hints, 0, sizeof(hints));

        if (argc != 2)
        {
                printf("Usage %s hostname/n",argv[0]);
                return;
        }

        err = getaddrinfo(argv[1], NULL, &hints, &res);
        if (!err)
        {
                int addno = 0;

                for (res1 = res; res1; res1 = res1->ai_next,addno++)
                {
                        printf("Address information of No %d/n",addno);

                        if (res1->ai_family == AF_INET)
                                printf("ai_family=%d,[%s]/n",res1->ai_family,"AF_INET");
                        if (res1->ai_family == AF_INET6)
                                printf("ai_family=%d,[%s]/n",res1->ai_family,"AF_INET6");
                        if (res1->ai_family == AF_UNSPEC)
                                printf("ai_family=%d,[%s]/n",res1->ai_family,"AF_UNSPEC");

                        if (res1->ai_protocol == IPPROTO_IP)
                                printf("ai_protocol=%d,[%s]/n",res1->ai_protocol,"IPPROTO_IP");
                        if (res1->ai_protocol == IPPROTO_IPV6)
                                printf("ai_protocol=%d,[%s]/n",res1->ai_protocol,"IPPROTO_IPV6");
                        if (res1->ai_protocol == IPPROTO_UDP)
                                printf("ai_protocol=%d,[%s]/n",res1->ai_protocol,"IPPROTO_UDP");
                        if (res1->ai_protocol == IPPROTO_TCP)
                                printf("ai_protocol=%d,[%s]/n",res1->ai_protocol,"IPPROTO_TCP");

                        printf("ai_socktype=%d/n",res1->ai_socktype);
                        printf("ai_flags=%d/n",res1->ai_flags);
                        printf("ai_addrlen=%d/n",res1->ai_addrlen);
                        printf("ai_canonname=%s/n",res1->ai_canonname==0?"null":res1->ai_canonname);
                        printf("/n");
                } /* for */

                freeaddrinfo(res);

        } /* if */
}

 

4. InetAddress.getByName(...)

    - java code used for both IPv4 and IPv6.

    - Please notice in a IPv4 and IPv6 mixed system, InetAddress.getByName(...) will return an IPv4 address, and C library getaddrinfo(...) will return first address as IPv6. So C library and Java library are not matched of their first address returned.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值