2008-07-10

Java Ping

**
	 * 能否ping通IP地址
	 * @param server IP地址
	 * @param timeout 超时时长
	 * @return true能ping通
	 */
	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;
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
评论
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

fantasy
搜索本博客
最近加入圈子
存档
最新评论