如何ping一个IP地址 [英] How to ping an IP address
本文介绍了如何ping一个IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这部分代码在 java 中 ping 一个 ip 地址,但只有 ping localhost 是成功的,对于其他主机,程序说该主机无法访问.我禁用了我的防火墙,但仍然有这个问题
I am using this part of code to ping an ip address in java but only pinging localhost is successful and for the other hosts the program says the host is unreachable. I disabled my firewall but still having this problem
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 请求
主机可达
发送 Ping 请求到 173.194.32.38
主机无法访问
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 173.194.32.38
Host is NOT reachable
推荐答案
你不能简单地在 Java 中 ping,因为它依赖于 ICMP,遗憾的是 Java 不支持
You can not simply ping in Java as it relies on ICMP, which is sadly not supported in Java
http://mindprod.com/jgloss/ping.html
改用套接字
希望能帮到你
这篇关于如何ping一个IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文