【发布时间】:2013-02-22 13:58:53
【问题描述】:
我在同一网络 (XP) 中有 2 个 Windows 系统。 如果我打开 cmd 并输入
ping Computer2
我得到了答案(所以 ping 工作正常)。 我以为我也可以用 Java 做到这一点,但不知何故它不起作用:
public static void ping() {
System.out.println("Ping Poller Starts...");
final String computer = "Computer2";
InetAddress inet = null;
try {
inet = InetAddress.getByName(computer);
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("Sending Ping Request to " + computer);
boolean status = false;
try {
status = inet.isReachable(5000);
} catch (IOException e) {
e.printStackTrace();
}
if (status)
{
System.out.println(computer + " ok");
}
else
{
System.out.println(computer + " not pingable");
}
}
总是not pingable。使用localhost 代码很好。但是使用 Computer2 我无法 ping - 但通过 cmd 它可以正常工作。有什么想法吗?
【问题讨论】:
-
你试过
www.stackoverflow.com吗? -
可以是很多东西。
isReachable()甚至不保证像ping命令那样使用 ICMP ECHO。尝试使用 IP 而不是“Computer2”。见docs.oracle.com/javase/7/docs/api/java/net/… -
@CongXu 不,它不工作......只是'localhost'通过Java工作。
标签: java networking ping