Domain Name Services

Page 1 2 3 4 5 6 7 8 9


SOURCE CODE

#include “stdio.h “
#include “netdb.h” /* This is the header file needed for gethostbyname () */
#include “sys/types.h”
#include “sys/socket.h”
#include “netinet/in.h”
#include “stdlib.h”

int main(int argc, char *argv[])
{
struct hostent *he;
if (argc!=2)
{
printf ("Usage: %s <>\n", argv[0]);
exit (-1);
}
if ((he=gethostbyname (argv[1]))==NULL)
{
printf ("gethostbyname() error\n");
exit (-1);
}
printf ("Hostname : %s\n", he->h_name); /* prints the hostname */
printf ("IP Address: %s\n",inet_ntoa(*((struct in_addr *)he->h_addr)));
}



OUTPUT:


[NPLab@localhost ~]$ cc dns21.c
[NPLab@localhost ~]$ ./a.out sys26.server1.com
Hostname : sys26.server1.com
IP Address: 170.100.140.125