Solved

Finding IP Address without knowing the Hostname

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

808 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