我记得在java network programming2(oreilly)一书说的
java是不可以实现ping的(p18页)
Slightly more of an issue is that Java does not provide direct access to the IP layer below TCP and UDP, so it can't be used to write programs such as ping or traceroute.
不知道你是不是说的这个ping
如果真的要实现的话,估计要调用native method了

解决方案 »

  1.   

    最简单的是调用系统的命令,否则还是用jni吧(不过这个我也不会)
      

  2.   

    到jbuilder里头,打开帮助,输入ping,有相关的程序。
      

  3.   

    谢谢各位的回答,真的很感谢,不过,上次我听同学说他上网的时候看到了有人用JAVA写了ping程序
    (顺便说,这个ping就是局域网内的ping),至于jbuilder,我还不会,不过,我会赶紧去看;谢谢各位的支持。
      

  4.   

    也许我可以用C 写,再接入JAVA中
      

  5.   

    不过,我还是求助各位,是否在JAVA中有写程序的方法呢,请赐教!谢谢你的支持。
      

  6.   

    java根本不支持icmp协议,所以用java实现ping是不可能的;唯一的办法是jni
      

  7.   

    直接用socket类去打造对象(给出IP地址),如果出错就是不通了
      

  8.   

    ping是UDP应用的实例,可能搞清楚UDP才行.
      

  9.   

    呵呵,给你一个思路参考,也不知道合不合你的要求,我自己调试通过
    ********************************************************
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    public class SystemCommand
    {
    /**
     * @param shellCommand
     * 
     */
    public List execute(String shellCommand)
    {
    try
    {
    start(shellCommand);
    List vResult= new ArrayList();
    DataInputStream in= new DataInputStream(p.getInputStream());
    BufferedReader reader= new BufferedReader(new InputStreamReader(in));
    String line=null;
    do
    {
    line= reader.readLine();
    if (line == null)
    {
    break;
    } else
    {
    vResult.add(line);
    }
    } while (true);
    reader.close();
    return vResult;
    } catch (Exception e)
    {
    //error
    return null;
    }
    }
    /**
     * @param shellCommand
     * 
     */
    public void start(String shellCommand)
    {
    try
    {
    if (p != null)
    {
    kill();
    }
    Runtime sys= Runtime.getRuntime();
    p= sys.exec(shellCommand);
    } catch (Exception e)
    {
    System.out.println(e.toString());
    }
    }
    /**
    kill this process
    */
    public void kill()
    {
    if (p != null)
    {
    p.destroy();
    p= null;
    }
    }
    private Process p=null;
    }
    ***********************************************************************]
    //在这里执行ping
    import java.util.List;
    import java.util.Iterator;
    public class exePing
    {
    //网卡物理地址长度
    static private final int MAC_LENGTH= 16;
    public static void main(String[] args)
    {
    System.out.println(getPing("172.18.10.10"));
    } static public String getPing(String ip){
    SystemCommand shell = new SystemCommand();
    String cmd = "cmd.exe /c ping "+ip;
    List result = shell.execute(cmd);
       return result.toString();
    }
    }