不好意思  不是什么icmp包 就是发送的 packet 呵呵   

解决方案 »

  1.   

    ping命令似乎只能用这样的方式实现
    你要得到发包收包的情况,可以在
    ping statistics 信息里得到
    另外你的
      int count = 10, index; 
      //最多只读10行 
    10行太少了
      

  2.   

    楼上发的什么图 看不见啊 呵呵 ping statistics 信息 怎么看啊? 
    或则有什么别当然方法?
      

  3.   

    icmp实现的话只能通过jni来实现,有开源jpcap
      

  4.   

    基本实现方法: 
    1. 利用操作系统的 ping 命令,再对输出进行分析。 
    2. 直接利用 ping 使用的ICMP协议,自己发包分析。
      

  5.   

    ping statistics for 10.64.2.7:     packets: sent = 4, received = 4, lost = 0 (0% loss), 上面的ping statistics信息里就有发包=4,收包=4,没有丢失的信息啊
      

  6.   

    package com; import java.io.BufferedReader; 
    import java.io.InputStreamReader; public class ICMP { 
        /** 
        * ping 的一种实现,调用操作系统的ping命令 
        */ 
        public static int ping(String host) { 
            String system = (String) (System.getProperty("os.name")).toLowerCase(); 
            String command = ""; 
            if (system.indexOf("win") != -1) { 
                command += "ping -n 5 " + host;// 可以设置ping 的次数 
            } else if (system.indexOf("linux") != -1) { 
                command += "ping -t 4 " + host; // ping 四次 
            } else { 
                command += "ping " + host; 
            } 
            int minTime = Integer.MAX_VALUE, curTime; 
            try { 
                Process process = Runtime.getRuntime().exec("ping " + host); 
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));      
                String line = null; 
                // 最多只读10行 
                while ((line = in.readLine()) != null ) { 
                    line = line.toLowerCase(); 
                    /* 
                    if ((index = line.indexOf("time")) != -1) { 
                        byte[] buf = line.getBytes(); 
                        int start = 0, end = buf.length, i, j; 
                        for (i = index + 4; i < buf.length; i++) { 
                            if (Character.isDigit((char) buf[i])) { 
                                start = i; 
                                break; 
                            } 
                        } 
                        if (i == buf.length) 
                            continue; 
                        for (j = start; j < buf.length; j++) { 
                            if (Character.isLetter((char) buf[j])) { 
                                end = j; 
                                break; 
                            } 
                        } 
                        curTime = Integer.parseInt(new String(buf, start, end - start)); 
                        if (curTime < minTime) { 
                            minTime = curTime; 
                        } 
                    } 
                    */ 
                    System.out.println(line); 
                } 
            } catch (Exception ex) { 
                return Integer.MAX_VALUE; 
            } 
            return minTime; 
        }     public static void main(String args[]) { 
            ICMP ic = new ICMP(); 
            System.out.println(ic.ping("10.64.2.7")); 
        } } 这是我改过的 运行后输出的信息为 pinging 10.64.2.7 with 32 bytes of data: 
    reply from 10.64.2.7: bytes=32 time <1ms ttl=252 reply from 10.64.2.7: bytes=32 time <1ms ttl=252 reply from 10.64.2.7: bytes=32 time <1ms ttl=252 reply from 10.64.2.7: bytes=32 time <1ms ttl=252 
    ping statistics for 10.64.2.7:     packets: sent = 4, received = 4, lost = 0 (0% loss), approximate round trip times in milli-seconds:     minimum = 0ms, maximum = 0ms, average = 0ms 请问 大虾门 有没 方法 可以将 直接取得 sent 和received 的 如果是这样的信息话  我想 要费好多事进行判断与截取了
    1..请问正则表达式  该怎么写  小弟没怎么接触过
      

  7.   


    package com; import java.io.BufferedReader; 
    import java.io.InputStreamReader; 
    import java.util.regex.*;public class ICMP { 
        /** 
        * ping 的一种实现,调用操作系统的ping命令 
        */ 
        public static String ping(String host) {
         StringBuffer result = new StringBuffer(); 
            String system = (String) (System.getProperty("os.name")).toLowerCase(); 
            String command = ""; 
            if (system.indexOf("win") != -1) { 
                command += "ping -n 5 " + host;// 可以设置ping 的次数 
            } else if (system.indexOf("linux") != -1) { 
                command += "ping -t 4 " + host; // ping 四次 
            } else { 
                command += "ping " + host; 
            }
            Pattern p = Pattern.compile("\\d(?=,)"); //获取send和receive的正则表达式
            int curTime; 
            try { 
                Process process = Runtime.getRuntime().exec("ping " + host); 
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));      
                String line = null; 
                // 最多只读10行 
                while ((line = in.readLine()) != null ) { 
                    line = line.toLowerCase(); 
                    System.out.println(line);
    Matcher m = p.matcher(line);
    while(m.find())
    {
    result.append(m.group() + " ");
    }
                } 
            } catch (Exception ex) { 
             System.out.println(ex);
            }
            return result.toString();
        }     public static void main(String args[]) { 
            ICMP ic = new ICMP(); 
            Pattern p = Pattern.compile("\\d+\\s+");
            String str = ic.ping("10.64.2.7");
            Matcher m = p.matcher(str);
            m.find();
            System.out.println("send: " + m.group());
            m.find();
            System.out.println("received: " + m.group());
        } }