get the host by address returning no host found in C
What am I doing wrong, when I pass an IP address, or any IP, it always
fails in the no host found block. Any assistance would be greatly
appreciated.
#include <stdio.h>
#include "unp.h"
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char** argv)
{
struct hostent *he;
struct in_addr **addr_list;
int shouldContinueFlag=0;
int numIpElements = sizeof(he->h_addr_list) / sizeof(he->h_addr_list[0]);
int len = strlen(argv[1]);
struct in_addr ip;
he = gethostbyname(argv[1]);
inet_aton(argv[1], &ip);
char *ip = argv[1];
he= gethostbyaddr((const void *)&ip,len,AF_INET);
if(he!=NULL)
{
printf("%s \n", he->h_name);
shouldContinueFlag=0; printf("The hostname mapped to the IP
address you passed is: \n");
}
else
{
printf("no host name associated with the IP address %s",
argv[1]); //CODE IS ALWAYS IN THIS LOOP!
}
}
No comments:
Post a Comment