2014-04-03 38 views
0

我想開發一個局域網搜索工具。爲此,我需要所有連接的主機IP。爲此,我正在使用它,但它對我無效。它會讓所有結果都無法訪問,即使是我自己的IP。在java中連接到局域網的所有主機的geting ip地址

請幫我什麼在這個

public static void checkHosts(String subnet) throws UnknownHostException, IOException{ 
    int timeout=1000; 
    for (int i=1;i<254;i++){ 
     System.out.println(i); 
    String host=subnet + "." + i; 
    if (InetAddress.getByName(host).isReachable(timeout)){ 
     System.out.println(host + " is reachable"); 
    } 
    else{ 
     System.out.println(host + " is not reachable"); 
    } 
} 

回答

1

走錯了你的代碼看起來不錯,嘗試增加超時

4s-5s超時會很棒,它至少應該顯示你自己的IP爲可達。


此外,isReachable的文件說:

盡力通過實施作出嘗試連接到主機, 但防火牆和服務器配置可能會阻止導致 一個無法訪問狀態請求。

因此,如果其他情況失敗,請確保沒有防火牆問題。

好運

相關問題