#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
Your code doesn't compile for me. I get errors on
in_addr
andhostent
. But if I change the declarations of those tostruct in_addr
andstruct hostent
respectively, it compiles without warnings and emits the following output when run: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.