C Program to Domain Name Service using UDP Protocol


  • To Domain Name Service (DNS) using UDP protocol.


  • PC with UNIX/ Linux Operating systems.
  • C compiler in Linux Environment


  • Start the program
  • Include necessary header files such as sys/socket.h, sys/types.h, neinet/in.h
  • Create the variable for predefined structure hostent
  • Get the members of the structure as Name, Address, Address type, Alias and address list with the
  • help of the structure variable.
  • Print all the output
  • End the program.


#include <sys/types.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> int main() { struct hostent *h; h=gethostbyname("www.amazon.com"); if(h==NULL) { herror("ERROR\n"); } else { printf("\n Name :%s ",h->h_name); printf("\n Address:%s ",inet_ntoa(*(struct in_addr*)h->h_addr)); printf("\n Addresstype is %d",h->h_addrtype); printf("\n Alias:%s",h->h_aliases[0]); printf("\n Address list:%s\n",inet_ntoa(*(struct in_addr *)h->h_addr_list[0])); } return 0; }
Code language: PHP (php)


Name :e15316.a.akamaiedge.net Address: Addresstype is 2 Alias:www.amazon.com Address list:
Code language: CSS (css)


The domain name service using UDP protocol was successfully verified.

Leave a Comment