Simply call a function to gethostbyname (),gethostbyname () returns a hostent structure pointer to the host name and address information that corresponds to the given host name. The declaration of the structure is consistent with the GETHOSTADDR (). Here is the function prototype:
under Windows platform
#include <winsock2.h>
struct hostent far *pascal far gethostbyname (const char
Far * name);
Name: A pointer to the host name.
under Linux platform
#include <netdb.h>
struct hostent *gethostbyname (const char * hostname);
Return: Non-null pointer--Success, NULL pointer--error, set H_errno
C Language Program implementation:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include < netdb.h> #include <sys/types.h> #include <netinet/in .h> int Main (int argc, char *argv[]) { Span class= "KWRD" >struct hostent *h; char hostname[40]; printf ( "Please enter the domain name \ n" ); scanf ( "%s" , hostname); GetChar (); if ((H=gethostbyname (hostname)) ==null) {fprintf (stderr, "Cannot get IP\n" ); Exit (1); } printf ( "HostName:%s\n" , H->h_name); printf ( "IP Address:%s\n" , Inet_ntoa (* ((struct in_addr *) h->h (_ADDR))); return exit_success;}