Gethostbyname()

en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Could some one tell me how to use gethostbyname() to convert the string entered by user into corresponding ip.If some one could show it by demonstrating a code that would be great.The user should get an option to enter the name of the site say google.com and the corresponding ip is shown and in case the user enters the ip it is converted in the site name.Thanks
0
0
en_7123's Avatar, Join Date: Feb 2010
Ambitious contributor
Quote:
Originally Posted by shabbir View Post
Ok My bad I'm using c on fedora.Could you please show by an example.Thanks
0
Gene Poole's Avatar, Join Date: Nov 2009
Contributor
Try this:

Code:
  struct hostent *hp;

  hp=gethostbyname(addr);
  if(hp){
    printf("%d.%d.%d.%d\n",hp->h_addr[0],hp->h_addr[1],hp->h_addr[2],hp->h_addr[3]);
  }