How to use gethostbyaddr() function

i meet a question.

i don't know how to use
struct hostent *gethostby(const void *addr, size_t length, int type)
function to get remote computer hostent struct information.:slight_smile:

#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
struct hostent *hp;
char *ip;
in_addr_t data;

if (argc = 2) {
ip = argv[1];
data = inet_addr(ip);
hp = gethostbyaddr(&data, 4, AF_INET);

if \(hp == NULL\) \{
	printf\("Unknown host\\n"\);
	exit\(1\);
\}//why did i always get hp = NULL;
else \{
	printf\("get remote computer hostent"\);	
\}

}
else printf("argv error");
}

can you give me a example?

thanks a lot!!

That works.

program 82.195.131.187

prints

get remote computer hostent

i am a freshman , i can't get it .
can you explain it?
thanks

inet_ntoa() converts a stringified IP address (as in dot-decimal) format to it's binary equivalent.

gethostbyaddr() then does a reverse name lookup to find out what name this address has.