这是一个简单的程序,我写它来找出域的所有A record
。Linux网络编程:getaddrinfo()得到错误的结果
我编制它,并没有得到任何错误或警告。
然后我运行它,我只发现它给出错误的IP,如:
./a.out www.google.com
2.0.0.0
2.0.0.0
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
int main(int argc, char *argv[])
{
struct addrinfo addrC;
struct addrinfo *addrL;
struct addrinfo *temp;
memset(&addrC, 0, sizeof(addrC));
addrC.ai_family = AF_INET;
addrC.ai_socktype = SOCK_STREAM;
addrC.ai_protocol = IPPROTO_TCP;
if (getaddrinfo(argv[1], "http", &addrC, &addrL) != 0)
{
perror("getaddrinfo!");
exit(1);
}
for (temp = addrL; temp != NULL; temp = temp->ai_next)
{
char addrBuf[BUFSIZ];
void *addrCount = &((struct sockaddr_in*)temp)->sin_addr;
inet_ntop(temp->ai_addr->sa_family, addrCount, addrBuf, sizeof(addrBuf));
printf("%s\n", addrBuf);
}
for (temp = addrL; temp != NULL; temp = addrL)
{
addrL = temp->ai_next;
free(temp);
}
return 0;
}
为什么?以及如何纠正它?
'struct addrinfo'不是'struct sockaddr_in',它们之间的投射可能会产生垃圾。您可以尝试使用'temp-> ai_addr',而不是'sockadr_in'。 (为什么你命名结果'addrCount'超出了我的意思,它是一个地址,而不是任何数字)。 – 2012-03-11 07:53:41