IP-address from hostname

IP-address from hostname

Post by Patrick Seelig » Sun, 29 Oct 1995 04:00:00



Hi there!

I'm writing a C++ program and need to get the IP address.
Assuming that you know the hostname, how can I get the
corresponding address? (Or could you recommend a good
reference book that has the solution? )

Thanks for any help.


 
 
 

IP-address from hostname

Post by Nigel Wa » Tue, 31 Oct 1995 04:00:00




>Hi there!

>I'm writing a C++ program and need to get the IP address.
>Assuming that you know the hostname, how can I get the
>corresponding address? (Or could you recommend a good
>reference book that has the solution? )

>Thanks for any help.



The routine you want is gethostbyname.  

How successful it is depends on how your system resolves names.
Provided you have a local entry for the host in question, or have access
to a DNS server you should be fine.  

If your  standard packages such as telnet and ftp can determine the host by
its name then gethostbyname is likely to work.

--
Nigel Wade, System Administrator, Ionospheric Physics Group,
            University of Leicester, Leicester, LE1 7RH, UK

Phone :     +44 (0)116 2523568, Fax : +44 (0)116 2523555

 
 
 

IP-address from hostname

Post by Salim Gasm » Tue, 31 Oct 1995 04:00:00


Hi ,

Well there is a simple C programm that display
official name, aliases and IP adresse(s) of hosts given as arguments

I advice you to read the : "UNIX Networks Programming" book
written by W.Richard STEVENS published by PRENTICE HALL
ISBN 0-13-949876-1

now come the source :

----------------------------------------------------------
#include <fcntl.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc,char **argv)
{
struct hostent *host; /* this is the struct for the infos of a host */
char *ptr;              
int i;
struct in_addr *ad;   /* and this to hold an IP adresse     */

for(i=1;i<argc;i++)
{

/* we get infos for the host */

host=gethostbyname(argv[i]);

/* we test if we succeded in getting infos */

if(host==NULL) printf("Unknown host %s\n",argv[i]);
else
 {
  printf("Official name for %s is %s\n",argv[i],host->h_name);

/* we display now the aliases list */

  while((ptr=*(host->h_aliases))!=NULL)
    {
    printf("alias for %s is : %s\n",argv[i],ptr);
    host->h_aliases++;
    }

/* we test if the adress of the host if from the INET familly */

    if(host->h_addrtype != AF_INET )
       printf("Unknown adress type for %s\n",argv[i]);

/* and display the list of IP adresses of the host

                                else
       while((ad=(struct in_addr*)*host->h_addr_list++)!=NULL)
          printf("IP for %s is : %s\n",argv[i],inet_ntoa(*ad));

  }

Quote:}
return(0);
}

----------------------------------------------------------

Hope this helps

Salim,

========================================================

"Quand le sage montre le ciel du doigt, l'idiot regarde le doigt"
"Un Clavier AZERTY en vaut deux"


Salim Gasmi

========================================================

 
 
 

IP-address from hostname

Post by Axel-Stephane Smorgr » Sat, 04 Nov 1995 04:00:00


|> Organization: University of Maryland College Park
|> Lines: 10

|> NNTP-Posting-Host: rac6.wam.umd.edu
|> X-Newsreader: TIN [version 1.2 PL0]
|>
|> Hi there!
|>
|> I'm writing a C++ program and need to get the IP address.
|> Assuming that you know the hostname, how can I get the
|> corresponding address? (Or could you recommend a good
|> reference book that has the solution? )

Use the gethostbyname() library function. Look at the man-pages
of your system.

 Axel-Stephane Smorgrav
Communications specialist
     OSLO - NORWAY