Solved

Finding IP Address without knowing the Hostname

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 302
ASP.net build a IF/Then Walkthrough Guide 1 209
is twain_32.dll cmpatible with windows 10 ? 10 161
C++ question 3 65
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 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