【问题标题】:Why am I not able to ping a computer?为什么我无法ping通计算机?
【发布时间】: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 它可以正常工作。有什么想法吗?

【问题讨论】:

标签: java networking ping


【解决方案1】:

不清楚您是在 ping IP 地址还是计算机名称,但可能很简单,例如使用“\Computer2”而不是“Computer2”。

【讨论】:

  • 我正在 ping 计算机名。使用 \Computer2 或在 Java 中“\\Computer2”不起作用:java.net.UnknownHostException
  • 那么ping IP地址怎么样?
  • 哦,这很有趣。即使使用 ip 也无法正常工作。只需通过 cmd。
  • 好的,接下来您应该尝试 ping 一个您知道有效的外部 IP 地址(尝试 173.194.34.102,即 google.com)。
  • 不,这甚至不能通过 cmd 工作。我无法 ping 一个地址。因此,即使通过 cmd,'ping www.stackoverflow.com' 或 'ping google.com' 也无法正常工作。也许这是网络有问题或者我没有权限。但正如我所说,我可以通过 cmd ping 'Computer2'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 2014-02-08
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
相关资源
最近更新 更多