I was trying to get the IP
address from an URL
but gethostbyname
always says Unknown host
. Hope someone can point out my fault.
Here is my code
char *handleAddress(char *URL)
{
struct hostent *he;
struct in_addr **addr_list;
int i;
char *ip = NULL;
printf("Search IP for this = %s\n", URL);
if ((he = gethostbyname(URL)) == NULL) // get the host info
{
herror("gethostbyname");
ip = "No Such DNS";
return ip;
}
addr_list = (struct in_addr **)he->h_addr_list;
for (i = 0; addr_list[i] != NULL; i++) //Return the first one;
{
strcpy(ip, inet_ntoa(*addr_list[i]));
return ip;
}
}
Expected: URL: www.google.com
return IP: 173.194.72.106