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

来源:互联网 发布:软件售后维保 编辑:程序博客网 时间:2024/05/03 16:09

程序:

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)

0 0
原创粉丝点击