Probe into the usage of gethostbyname () and getaddrinfo ()

Source: Internet
Author: User

The main is how to use the return structure of two functions. In the GetHostByName function, the return Hostent,linux reference manual is described below:

struct hostent {    char  *h_name;            /* official name of host */    char **h_aliases;         /* alias list */    int    h_addrtype;        /* host address type */    int    h_length;          /* length of address */    char **h_addr_list;       /* list of addresses */}

The specific usage can be seen in the following code:

#include <arpa/inet.h>#include <netdb.h>#include <stdlib.h>#include <stdio.h>intMainintargcChar* argv[]) {Char*ptr, **pptr;CharStr[inet_addrstrlen];structHostent * HPTR; while(--ARGC >0) {ptr = *++argv;if((hptr = gethostbyname (ptr)) = = NULL) {fprintf(stderr,"gethostbyname error for host%s:%s", PTR, Hstrerror (H_errno));Continue; }printf("Official hostname%s\n", hptr->h_name); for(pptr = hptr->h_aliases; *pptr! = NULL; ++pptr)printf("\talials:%s\n", *pptr);Switch(Hptr->h_addrtype) { CaseAf_inet:pptr = hptr->h_addr_list; for(; *pptr! = NULL; ++pptr)printf("\taddress:%s\n", Inet_ntop (Hptr->h_addrtype, *pptr, str,sizeof(str))); Break;default:fprintf(stderr,"Unknown address type");Exit(exit_failure); Break; }    }return 0;}

./hostent www.baidu.comafter execution
The effect is as follows:

The Getaddrinfo function is more complex, but provides a better interface.
This is just a sample program to show.

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/socket.h>#include <netdb.h>#include <sys/types.h>#include <arpa/inet.h>voidPrintaddressinfo (Const structaddrinfo*);intMainintargcChar* argv[]) {intNstructaddrinfo* Res, *ressave;structAddrinfo hint;Char* SERV;if(ARGC <2)    {fprintf(stderr,"Usage:"                "./getaddrinfo                 "or Port name>]\n");Exit(exit_failure); }if(ARGC = =2) serv = NULL;Elseserv = argv[2]; Bzero (&hint,sizeof(hint)); hint.ai_family = Af_unspec;if((n = getaddrinfo (argv[1], serv, &hint, &res)! =!0)    {if(Argc >2)fprintf(stderr,"Cannot get information for"                    "%s:%s\n\terror:%s\n", argv[1], serv, Gai_strerror (n));Else            fprintf(stderr,"Cannot get information for"                    "%s\n\terror:%s\n", argv[1], Gai_strerror (n));Exit(exit_failure); } ressave = res; while(res! = NULL)        {Printaddressinfo (res);    res = res->ai_next; } freeaddrinfo (Ressave);Exit(exit_success);}voidPrintaddressinfo (Const structaddrinfo* addr) {CharStr[inet6_addrstrlen]; ShortPortprintf("ai_family:");Switch(addr->ai_family) { CaseAf_inet: {structSockaddr_in* in;printf("ipv4\n"); in = (structsockaddr_in *) (ADDR-&GT;AI_ADDR);            Port = Ntohs (In->sin_port); Inet_ntop (af_inet, &in->sin_addr, str,sizeof(str)); Break; } CaseAf_inet6: {structsockaddr_in6* in6; In6 = (structSOCKADDR_IN6 *) (ADDR-&GT;AI_ADDR);            Port = Ntohs (In6->sin6_port); Inet_ntop (AF_INET6, &in6->sin6_addr, str,sizeof(str));printf("ipv6\n"); Break; }default:printf("unknown\n"); Break; }printf("Ai_socktype:");Switch(Addr->ai_socktype) { CaseSock_stream:printf("stream\n"); Break; CaseSock_dgram:printf("dgram\n"); Break; CaseSock_seqpacket:printf("seqpacket\n"); Break; CaseSock_raw:printf("raw\n"); Break;default:printf("others\n"); Break; }printf("Protocol:");Switch(Addr->ai_protocol) { CaseIPPROTO_TCP:printf("Tcp\n"); Break; CaseIPPROTO_UDP:printf("Udp\n"); Break; CaseIPPROTO_SCTP:printf("Sctp\n"); Break;default:printf("others\n"); Break; }printf("Address:%s\n", str);printf("Port:%d\n", port);printf("canonical name:%s\n\n", addr->ai_canonname);}

The above procedure after entering: ./getaddrinfo www.baidu.com http , the effect is as follows:

The focus is on the display of the address. The contents of the structure.

Probe into the usage of gethostbyname () and getaddrinfo ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.