While playing around with getting host entries to work with ldap, I found that my previous code for using gethostbyname was not running clean on puias6. Here is an updated version
<br /> #include <stdio.h><br /> #include <netdb.h><br /> #include <stdlib.h><br /> #include <sys/socket.h><br /> #include <netinet/in.h><br /> #include <arpa/inet.h></p> <p>int main(int argc, char **argv) {<br /> int i;<br /> struct hostent *hp;<br /> struct in_addr **addr_list;</p> <p> if (argc < 2) {<br /> printf("Usage: %s hostname\n", argv[0]);<br /> exit(1);<br /> }</p> <p> hp = gethostbyname(argv[1]);<br /> if (hp == NULL) {<br /> printf("gethostbyname(%s) failed\n",argv[1]);<br /> } else {<br /> printf("%s has address ", hp->h_name, argv[1],hp->h_addrtype);<br /> addr_list = (struct in_addr **)hp->h_addr_list;</p> <p> for(i = 0; addr_list[i] != NULL; i++) {<br /> printf("%s ",inet_ntoa(*addr_list[i]));<br /> }<br /> printf("\n");<br /> }<br /> }<br />
compile this with gcc -o gethostbyname gethostbyname.c