DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Gethostbyname

02.09.2006
| 270 views |
  • submit to reddit
        
#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>

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

   int i;
   struct hostent *host;
   host = (argc > 1) ? gethostbyname(argv[1]) : gethostbyname("s2.enemy.org");

   if (!host) {
      perror("Invalid Address");
      exit(0);
   }

   printf("Addresses for %s\n", host->h_name);

   i=0;
   while (host->h_addr_list[i]) {
      printf("\t%s\n",
         inet_ntoa(*(struct in_addr *)host->h_addr_list[i]));
      i++;
   }
   return;
}

    
    Tags:
  • C