Solved

Finding IP Address without knowing the Hostname

Posted on 2000-05-17
3
267 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

867 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

22 Experts available now in Live!

Get 1:1 Help Now