开发者

getaddrinfo() function returns the wrong IP address

I am 开发者_运维技巧trying to resolve a URL's IP address using getaddrinfo(), but it always returns the wrong IP address, I have tried with several URL's and the result is same. any help wold be greatly appreciated.

The program returnes the ip address : 209.85.175.99 insted of returning the real IP which is 74.125.39.147

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

int
main()
{
 char *hostname = "www.google.lk";
 struct addrinfo hints, *res;
 struct in_addr addr;
 int err;

 memset(&hints, 0, sizeof(hints));
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_family = AF_INET;

 if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0) {
 printf("error %d\n", err);
 return 1;
 }

 addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;

 printf("ip address : %s\n", inet_ntoa(addr));

 freeaddrinfo(res);

 return 0;
}


google.com can resolve to different IP addresses depending on your own location. It's kind of load balancing trick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜