Solved

Finding IP Address without knowing the Hostname

Posted on 2000-05-17
3
266 Views
Last Modified: 2010-04-02
If I cannot get the hostname from gethostname, is there another way to get the IP address that is independent of knowing the hostname?
0
Comment
Question by:paulca
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 30 total points
ID: 2818674
If you cannot determine the hostname, IP isn't configured correctly (->'gethostname()'). But as every host has a loopback IF (i.e. IP 127.0.0.1), you can use this one in a call to 'gethostbyaddr()' and extract the other addresses form the 'h_addr_list' member of the 'HOSTENT' struct returned by this call.
0
 

Author Comment

by:paulca
ID: 2823521
The code I'm using is which gives me an error:

pHost = gethostbyaddr("127.0.0.1", 4, PF_INET);

for(j=0;j<pHost->h_length;j++) {

                        if(j>0) {
                              strcat(addrStrings, ".");
                        }
                        sprintf(buf, "%u", (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
                        strcat(addrStrings, buf);
                  }
                        printf (addrStrings);
0
 
LVL 86

Expert Comment

by:jkr
ID: 2827429
Ooops, it seems that the comment I posted some hours ago is gone...

So, again:

The addresses are stored as DWORDs in network byte order - use sth like

char    *BinIP2StrIP    (   long    lnIP)
{
    static char     s_acStrIP   [   16];

    PUCHAR          puc =   ( unsigned char *) &lnIP;
    unsigned int    aun [   4];

    for (   int i = 0;  i < 4;  aun [   i++]    =   *puc++);

    sprintf (   s_acStrIP,
                "%d.%d.%d.%d",
                aun [   0],
                aun [   1],
                aun [   2],
                aun [   3]
            );

    return  (   s_acStrIP);
}


DWORD dwIP;
for(j=0;dwIP = pHost->h_addr_list [j];j++)
{
 printf ( "%s", BinIP2StrIP ( dwIP));
}

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now