我求解java ping程序,即如何在java中实现ICMP??
谢谢各位的帮忙.

解决方案 »

  1.   

    /**
         * ping the server
         * @param server String
         * @param timeout int
         * @return boolean
         * @throws IOException
         */
        public static boolean pingServer(String server,int timeout)
        {
            BufferedReader in = null;
            Runtime r = Runtime.getRuntime();        String pingCommand = "ping " + server + " -n 1 -w " + timeout;
            try
            {
                Process p = r.exec(pingCommand);
                if (p == null)
                {
                    return false;
                }
                in = new BufferedReader(new InputStreamReader(p.
                    getInputStream()));
                String line = null;
                while ( (line = in.readLine()) != null)
                {
                    if (line.startsWith("Reply from"))
                    {
                        return true;
                    }
                }
                in.close();
            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
        }