注册 登录
  • 欢迎访问Sharezer Blog

Java ping 网络状态

JAVA sharezer 386次浏览 已收录 0个评论
public void testPing() {
   final Thread pingThread = new Thread() {
       @Override
       public void run() {
           try {
               String lost = new String();
               String delay = new String();
               Process p = Runtime.getRuntime().exec("ping -c 4 " + "www.baidu.com");
               BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
               String str = new String();
               while ((str = buf.readLine()) != null) {
                   if (str.contains("packet loss")) {
                       int i = str.indexOf("received");
                       int j = str.indexOf("%");
                       Log.d("ping", "丢包率:" + str.substring(i + 10, j + 1));
//                  System.out.println("丢包率:"+str.substring(j-3, j+1));
                       lost = str.substring(i + 10, j + 1);
                   }
                   if (str.contains("avg")) {
                       int i = str.indexOf("/", 20);
                       int j = str.indexOf(".", i);
                       Log.d("ping", "延迟:" + str.substring(i + 1, j));
                       delay = str.substring(i + 1, j);
                       delay = delay + "ms";
                   }
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   };
   pingThread.start();

}

Sharezer , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Java ping 网络状态
喜欢 (0)
[liangshaoze@sina.cn]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址