我正在使用这部分代码在java中ping一个ip地址,但只有ping本地主机成功,而对于其他主机,程序显示主机是无法访问的。我禁用了防火墙,但仍然有这个问题
public static void main(String[] args) throws UnknownHostException, IOException {
String ipAddress = "127.0.0.1";
InetAddress inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
ipAddress = "173.194.32.38";
inet = InetAddress.getByName(ipAddress);
System.out.println("Sending Ping Request to " + ipAddress);
System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
}
输出为:
向127.0.0.1发送Ping请求
主机可访问
向173.194.32.38发送Ping请求
主机无法访问
发布于 2012-07-16 23:48:30
发布于 2012-07-17 00:16:38
javadoc中的InetAddress.isReachable()
如果可以获得权限,典型的实现将使用
回显请求,否则将尝试在目标主机的端口7(回显)上建立连接。
选项1 (ICMP)通常需要管理(root)
权限。
发布于 2015-04-06 02:26:59
我认为这段代码会对你有所帮助:
public class PingExample {
public static void main(String[] args){
try{
InetAddress address = InetAddress.getByName("192.168.1.103");
boolean reachable = address.isReachable(10000);
System.out.println("Is host reachable? " + reachable);
} catch (Exception e){
e.printStackTrace();
}
}
}
https://stackoverflow.com/questions/11506321
复制相似问题