马上要交作业了,必须交一个ping程序的java实现,哪位大侠帮帮我啊,发个能运行的ping程序给我。谢谢你们了   [email protected]

解决方案 »

  1.   

    import java.io.*;public class Ping
    {
        public static void main(String args[])
        {
            String str = "127.0.0.1";
            String line = null;
            try
            {
                Process pro = Runtime.getRuntime().exec("ping " + str);
                BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
                while ((line = buf.readLine()) != null)
                    System.out.println(line);
            }
            catch (Exception ex)
            {
                System.out.println(ex.getMessage());
            }
        }}
      

  2.   

    ping是基于icmp协议的,而java是在tcp和udp协议层上的。
    所以不能直接用Java实现Ping功能。
      

  3.   

    import java.io.IOException;
    import java.io.InputStream;public class TestBat {
        public static void main(String[] args) {
            String command = "c:\\demo.bat";
            try {
                Process child = Runtime.getRuntime().exec(command);
                InputStream in = child.getInputStream();
                int c;
                while ((c = in.read()) != -1) {
                    System.out.print(c);//如果你不需要看输出,这行可以注销掉
                }
                in.close();
                try {
                    child.waitFor();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("done");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
      

  4.   

    哈哈,这样可以吗?很偷懒的思路,直接调用系统自带的pingimport java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class pingFake {    public static void main(String[] args) {        String ip = args[0];
            String pingResult = "";        String pingCmd = "ping " + ip;        try {
                Runtime r = Runtime.getRuntime();
                Process p = r.exec(pingCmd);            BufferedReader in = new BufferedReader(new InputStreamReader(p
                        .getInputStream()));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println(inputLine);
                    pingResult += inputLine;
                }
                in.close();        } catch (IOException e) {
                System.out.println(e);
            }
        }
    }output:
    java pingFake 127.0.0.1Pinging 127.0.0.1 with 32 bytes of data:Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Ping statistics for 127.0.0.1:
        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 0ms, Maximum = 0ms, Average = 0ms
      

  5.   

    Runtime.getRuntime().exec("cmd /c start ping 192.168.1.1");
      

  6.   

    没什么说的,一楼的就基本可以满足要求了,作业嘛,不可能要求那么高,还要去管协议的东西~直接调系统的命令就行了,呵呵   Runtime.getRuntime().exec("cmd /c ping 192.168.1.1");
      

  7.   

    Integer o1=new Integer(10); 
    Integer o2=new Integer(10); 
      

  8.   

    import java.io.IOException;
    import java.io.InputStream;public class TestBat {
        public static void main(String[] args) {
            String command = "c:\\demo.bat";
            try {
                Process child = Runtime.getRuntime().exec(command);
                InputStream in = child.getInputStream();
                int c;
                while ((c = in.read()) != -1) {
                    System.out.print(c);//如果你不需要看输出,这行可以注销掉
                }
                in.close();
                try {
                    child.waitFor();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("done");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }看不到ping 的结果就是