浏览 2915 次
锁定老帖子 主题:Java Ping
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-07-10  
**
	 * 能否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;
    }
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics