2012-09-08 58 views
0
#include <stdio.h> 
#include <netdb.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 

int main() { 
    in_addr ip; 
    ip.s_addr = inet_addr("173.194.71.94"); // www.google.fr IP 
    hostent* hostnames = gethostbyaddr(&ip, sizeof(ip), AF_INET); 
    if (hostnames != NULL && hostnames[0].h_name != NULL) { 
     printf("%s\n", hostnames[0].h_name); 
     return 0; 
    } else { 
     herror("gethostbyaddr"); 
     return 1; 
    } 
} 

它返回「gethostbyaddr:未知主機」。我嘗試了不同的IP。怎麼了 ?gethostbyaddr錯誤未知主機

任何人都可以幫助我嗎? 謝謝

回答

0

您的代碼不能編譯我。我在in_addrhostent上遇到錯誤。但是,如果我分別改變這些的聲明來struct in_addrstruct hostent,它編譯沒有警告和運行時發出的輸出如下:

lb-in-f94.1e100.net 

這似乎是正確的。

如果它按原樣編譯,那麼您可能使用的是不同的操作系統。我以相同的結果嘗試了MacOS和Linux。

+0

此代碼來自不需要「結構」關鍵字的C++代碼。我在Mac OS X 10.6上,它不起作用(一個懶惰的等待,然後錯誤) –

+0

「* ... looooong等... *」:這聽起來像一個DNS超時。 @FélixFaisant – alk

+0

是的,這是一個超時。我在Fedora 17下的另一臺計算機上測試了這個編程,它的工作原理。所以代碼是好的... –