可以ping通,而InetAddress isReachable卻返回false

程序:

package xxx.xxx;


import org.junit.Test;
import java.io.IOException;
import java.net.InetAddress;

public class InetAddressTest {

    @Test
    public void testIsReachable() {
        try {
            System.out.println("testing................................");
            System.out.println(InetAddress.getByName("xxx.xxx.xxx.xxx").isReachable(10));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


原因:

查看啓動程序的賬戶爲user,非root,而isReachable使用icmp實現此功能,需要root權限


參考:

http://stackoverflow.com/questions/4779367/problem-with-isreachable-in-inetaddress-class

(read the javadoc, it requires such privileges. Basically to use ICMP (raw socket), it does take 'root'. And if you ask why ping from bash doesn't, actually it does need as well. Do that ls -l /bin/ping)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章