gethostbyaddr error Unknown host

1.7k Views Asked by At
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main () {
    in_addr ip;
    ip.s_addr = inet_addr("173.194.71.94"); // www.google.fr IP
    hostent* hostnames = gethostbyaddr(&ip, sizeof(ip), AF_INET);
    if (hostnames != NULL && hostnames[0].h_name != NULL) {
        printf("%s\n", hostnames[0].h_name);
        return 0;
    } else {
        herror("gethostbyaddr");
        return 1;
    }
}

It returns "gethostbyaddr: Unknown host". I tried with differents IPs. What's wrong ?

Can anybody helps me ? Thanks

1

There are 1 best solutions below

3
On

Your code doesn't compile for me. I get errors on in_addr and hostent. But if I change the declarations of those to struct in_addr and struct hostent respectively, it compiles without warnings and emits the following output when run:

lb-in-f94.1e100.net

which seems correct.

If it compiles as-is for you, then you are probably using a different operating system. I tried MacOS and Linux with identical results.