【发布时间】:2012-07-15 10:04:05
【问题描述】:
我正在使用这部分代码在 java 中 ping 一个 IP 地址,但只有 ping localhost 是成功的,对于其他主机,程序说主机无法访问。 我禁用了我的防火墙,但仍然有这个问题
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 请求
主机无法访问
【问题讨论】:
-
如果您直接使用 ping,是否可以 ping 该服务器?
-
你在 jTextField1 中输入了什么?
-
是的!但在我的程序中,我只能 ping localhost !!!
-
@user1529128 我已经用一个简单的示例编辑了您的问题,该示例重现了您描述的行为。如果您认为这不是您所要求的,请随时回滚编辑。
-
@assylias:感谢您的精彩编辑!我是 stackoverflow 的新手,这是我的第一个问题。谢谢你帮助我。