getaddrinfo


The following program demonstrates how you might use the getaddrinfo C library call to initialize a struct sockaddr in preparation for connecting to a server. The program is invoked as:


      ./getaddrinfo HOST PORT
      

where HOST is a domain name. Rather than connect to HOST, the program simply prints the IPv4 and IPv6 addresses that HOST resolves to. The getaddrinfo returns these addresses in a linked list, which must be deallocated using the C library function freeaddrinfo. Note that if getaddrinfo is unable to resolve the domain name to an IP address, the function gai_strerror can be used to convert the error value to a string.

getaddrinfo.c