Ping函数返回所有ping过的IP地址是可达的 [英] Ping function returns that all pinged IP addresses is reachable

查看:212
本文介绍了Ping函数返回所有ping过的IP地址是可达的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算ping从192.168.1.1到192.168.1.254的IP地址。首先,我使用的是InetAddress类,但它被窃听,有些IP地址即使是不可达也是如此。之后,我尝试了这种方法,它对单个IP工作非常好,但是当我把它放在for-loop里面的时候,所有ping到的IP都可以到达。你们可以告诉我这里有什么问题吗?

CODE:

  public class Main {
public static void main(String [] args) {
String ip =192.168.1。;
尝试
{
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 nothable:+ ip2);

$ b} catch(Exception e)
{
e.printStackTrace();




code $


<编辑1:



我使用内置的Java函数来执行ping操作,但它不工作(再次)

这里是我使用的代码

  import java.io.IOException; 
导入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(发送Ping请求到+ ip);
if(inet.isReachable(5000)){
System.out.println(ip +is reachable);
}
else {
System.out.println(ip +is not reachable);



$ b $ / code $ / pre
$ b $ p OUTPUT IS :

 发送Ping请求到192.168.1.243 
192.168.1.243不可达
$ b pre
$ b

这里是ping结果,当我ping从Windows 7内置Ping功能(CMD)



解决方案

使用

 <$ c $>  C> InetAddress.getByName(地址).isReachable(超时); 


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?

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:

I used built in Java function to preform pinging but it's not working (again)

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)

解决方案

Use isReachable() instead.

InetAddress.getByName(address).isReachable(timeout);

这篇关于Ping函数返回所有ping过的IP地址是可达的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆