C/C++ : How to retrieve a machine name from IP address?

Hi,

Could you please tell me how to retrieve the machine name using its IP address. I know if I have machine name I can retrieve IP address using gethostbyname but is there any way to retrieve machine name if I have IP address? The problem here is when client connect to server, server will have client IP address but I want  to display client's machine name along with IP address. For that I need to get the machine name from IP address. I think there must be some way to get this using DNS lookup but I am not sure,.

Thanks.
LVL 1
deshawAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You'd use 'gethostbyaddr()' (http://msdn.microsoft.com/en-us/library/ms738521.aspx) in that case. E.g. (like on the MSDN page):
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
 
int main(int argc, char **argv)
{
 
    //-----------------------------------------
    // Declare and initialize variables
    WSADATA wsaData;
    int iResult;
 
    DWORD dwError;
 
    struct hostent *remoteHost;
    char *host_name;
    struct in_addr addr;
 
    // Validate the parameters
    if (argc != 2) {
        printf("usage: %s ipv4 address\n", argv[0]);
        printf("  to return the host\n");
        printf("       %s 127.0.0.1\n", argv[0]);
        return 1;
    }
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }
 
    host_name = argv[1];
 
// If the user input is an alpha name for the host, use gethostbyname()
// If not, get host by addr (assume IPv4)
    if (isalpha(host_name[0])) {        /* host address is a name */
        printf("Calling gethostbyname with %s\n", host_name);
        remoteHost = gethostbyname(host_name);
    } else {
        printf("Calling gethostbyaddr with %s\n", host_name);
        addr.s_addr = inet_addr(host_name);
        if (addr.s_addr == INADDR_NONE) {
            printf("The IPv4 address entered must be a legal address\n");
            return 1;
        } else
            remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
    }
 
    if (remoteHost == NULL) {
        dwError = WSAGetLastError();
        if (dwError != 0) {
            if (dwError == WSAHOST_NOT_FOUND) {
                printf("Host not found\n");
                return 1;
            } else if (dwError == WSANO_DATA) {
                printf("No data record found\n");
                return 1;
            } else {
                printf("Function failed with error: %ld\n", dwError);
                return 1;
            }
        }
    } else {
        printf("Function returned:\n");
        printf("\tOfficial name: %s\n", remoteHost->h_name);
        printf("\tAlternate names: %s\n", remoteHost->h_aliases);
        printf("\tAddress type: ");
        switch (remoteHost->h_addrtype) {
        case AF_INET:
            printf("AF_INET\n");
            break;
        case AF_INET6:
            printf("AF_INET\n");
            break;
        case AF_NETBIOS:
            printf("AF_NETBIOS\n");
            break;
        default:
            printf(" %d\n", remoteHost->h_addrtype);
            break;
        }
        printf("\tAddress length: %d\n", remoteHost->h_length);
        addr.s_addr = *(u_long *) remoteHost->h_addr_list[0];
        printf("\tFirst IP Address: %s\n", inet_ntoa(addr));
    }
 
    return 0;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deshawAuthor Commented:
Will it works only for IPV4 addresses and not for APV6 addresses?

Thanks,
0
jkrCommented:
Well, 'gethostbyaddr()' works for both address families, depending on which parameter you pass as the last parameter - that can be AF_INET for IPv4 or AF_INET6 for IPv6.
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

deshawAuthor Commented:
Could you tell me why second argument is only 4 gethostbyaddr((char *) &addr, 4, AF_INET)?
0
deshawAuthor Commented:
How would I will get whether machine has IPV4 or IPV6 addresses then?

Thanks,
0
jkrCommented:
'4' is the length, in bytes, of the address. That should better be written as

gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);

You can tell what address family the remote host belongs to when you get an error code returned, indicating that it is the other address family on the remote side.
0
deshawAuthor Commented:
Thanks ikr.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.