Java Ping

 1 **
 2      * 能否ping通IP地址
 3      * @param server IP地址
 4      * @param timeout 超时时长
 5      * @return true能ping通
 6      */
 7     public static boolean pingServer(String server, int timeout) {
 8         BufferedReader in = null;
 9         Runtime r = Runtime.getRuntime();
10 
11         String pingCommand = "ping " + server + " -n 1 -w " + timeout;
12         try {
13             Process p = r.exec(pingCommand);
14             if (p == null) {
15                 return false;
16             }
17             in = new BufferedReader(new InputStreamReader(p.getInputStream()));
18             String line = null;
19             while ((line = in.readLine()) != null) {
20                 if (line.startsWith("Reply from")) {
21                     return true;
22                 }
23             }
24 
25         } catch (Exception ex) {
26             ex.printStackTrace();
27             return false;
28         } finally {
29             try {
30                 in.close();
31             } catch (IOException e) {
32                 e.printStackTrace();
33             }
34         }
35         return false;
36     }

来自 :http://kiral.iteye.com/blog/213487

转载于:https://www.cnblogs.com/XiaoGer/archive/2012/08/13/2636398.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值