這是一個簡單的程序,我寫它來找出域的所有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