java Ping 函数返回所有 ping 的 IP 地址均可访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16972206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ping function returns that all pinged IP addresses is reachable
提问by ZhiZha
I am tring to ping IP addresses from 192.168.1.1 to 192.168.1.254. First I was using I InetAddress class but it was bugged and some IPs where not reachable even if they are. After that I tried this method and it worked very well for single ping IP but when I put it inside for-loop all pinged IPs where reachable... Can you guys tell me what's wrong here?
我正在尝试 ping 从 192.168.1.1 到 192.168.1.254 的 IP 地址。首先,我使用的是 InetAddress 类,但它被窃听了,并且一些 IP 无法访问,即使它们是。在那之后,我尝试了这种方法,它对单个 ping IP 工作得很好,但是当我把它放在 for-loop 中时,所有 ping 的 IP 都可以访问......你们能告诉我这里有什么问题吗?
CODE:
代码:
public class Main {
public static void main(String[] args) {
String ip="192.168.1.";
try
{
for(int i=0;i<=254;i++){
String ip2=ip+i;
boolean reachable = (java.lang.Runtime.getRuntime().exec("ping -n 1 "+ip2).waitFor()==0);
if(reachable){
System.out.println("IP is reachable:: "+ip2);
}
else{
System.out.println("IP is not reachable: "+ip2);
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
EDIT 1:
编辑 1:
I used built in Java function to preform pinging but it's not working (again)
我使用内置的 Java 函数来执行 ping,但它不起作用(再次)
here is code that I used
这是我使用的代码
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test {
public static void main(String[] args) throws UnknownHostException, IOException {
String ip = "192.168.1.243";
InetAddress inet = InetAddress.getByName(ip);
System.out.println("Sending Ping Request to " + ip);
if (inet.isReachable(5000)){
System.out.println(ip+" is reachable");
}
else{
System.out.println(ip+" is not reachable");
}
}
}
OUTPUT IS:
输出是:
Sending Ping Request to 192.168.1.243
192.168.1.243 is not reachable
Also here is ping result when I do pinging from Windows 7 built in Ping function (cmd)
当我从 Windows 7 内置 Ping 函数 (cmd) 执行 ping 时,这里也是 ping 结果
回答by BlueRaja - Danny Pflughoeft
回答by Simon Forsberg
Why it does not work:
为什么它不起作用:
You are using the exit status of the ping process, not the actual result of the ping itself. It will just tell you whether or not the process exited normally or not. A failed ping does not cause the process to exit abnormally, and thus an exit code of 0 (zero) is always returned.
您使用的是 ping 过程的退出状态,而不是 ping 本身的实际结果。它只会告诉您进程是否正常退出。失败的 ping 不会导致进程异常退出,因此始终返回退出代码 0(零)。
What you could try instead:
你可以尝试什么:
Get the output stream of the process, which will tell you what the output is. Then try to interpret/parse this however you would like:
获取进程的输出流,它会告诉你输出是什么。然后尝试解释/解析你想要的:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html#getOutputStream%28%29
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html#getOutputStream%28%29
(Although this also doesn't feel perfect for me, it is a much better choice than using the exit code)
(虽然这对我来说也不是很完美,但它是比使用退出代码更好的选择)
回答by Ahmed kijazi from Tanzania
Dear sir The issue why you program cannot ping systems over the loop is, the execution of the loop is faster compared to the replies from the systems.That is why some of them replies Unreachable, in order to solve such problem you are supposed to use a thread and introduce a little delay on each ping by using Thread.sleep() method.I think this will work Thank you
亲爱的先生您的程序无法通过循环 ping 系统的问题是,与来自系统的回复相比,循环的执行速度更快。这就是为什么其中一些回复无法访问的原因,为了解决您应该使用的此类问题一个线程并通过使用 Thread.sleep() 方法在每个 ping 上引入一点延迟。我认为这会起作用 谢谢