티스토리 뷰

gethostbyname() 예제

#include<stdio.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<stdlib.h>

int main(int argc, char **argv)
{
   struct hostent *hptr;
   char **aptr ;
   int i=0;

   hptr = gethostbyname(argv[1]);

   if ( argc != 2 )
   {
      printf("Type <Command> and <Domain Name>\n") ;
      exit(1);
   }

   if ( hptr == NULL )
   {
      printf("gethostbyname() error!\n") ;
   }

   printf("Official Host Name : %s\n", hptr->h_name ) ;
 
   for ( i = 0 ; hptr->h_aliases[i] != NULL ; i++)
      printf("Alias Host Name : %s\n", hptr->h_aliases[i] ) ;

   for ( i = 0 ; hptr->h_addr_list[i] != NULL ; i++)
      printf("IP Address : %s\n", inet_ntoa(*(struct in_addr*)hptr->h_addr_list[i]));
}


신고

Recent Comments