小编Meh*_*hdi的帖子

如何ping IP地址

我正在使用这部分代码来ping java中的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");
}
Run Code Online (Sandbox Code Playgroud)

输出是:

发送Ping请求到127.0.0.1
主机可达
发送Ping请求到173.194.32.38无法
访问主机

java host ping

73
推荐指数
7
解决办法
23万
查看次数

标签 统计

host ×1

java ×1

ping ×1