android ping function

 public int pingIP() {
  int sumValues = 0;
  try {
   if(pbuilder == null){
    pbuilder = new ProcessBuilder();
    pbuilder.command(PING_COMMAND,mPingSepcifyIp);
    pbuilder.redirectErrorStream(true);
    process = pbuilder.start();
   }
   
   in = process.getInputStream();
   BufferedReader br = new BufferedReader(new InputStreamReader(in));
   String str = null;
   int times = 0;
   while ((str = br.readLine()) != null) {
    if (times > ALLOW_PING_TIMES) {
     DebugLog.d(TAG, "process  break while");
     break;
    }
    Log.i(TAG, str+"\n");
    int indexBegin = str.lastIndexOf(PING_FIND_KEY_BEGIN);
    int indexEnd = str.lastIndexOf(PING_FIND_KEY_END);
    Log.i(TAG, "indexBegin="+indexBegin+",indexEnd="+indexEnd+",length="+str.length());
    if(indexBegin > 0 && indexEnd >0){
     String newStr = (str.substring(indexBegin+1, indexEnd)).trim();
     Log.e(TAG, "new string = "+newStr);
     if(newStr != null || !"".equals(newStr)){
      if(newStr.contains(".")){//avoid pointer
       String strBuf[] = newStr.split("\\.");
       DebugLog.d(TAG, "StrBuf length="+strBuf.length);
       newStr = strBuf[0];
      }
      int cNumber = 0;
      try{
       cNumber = Integer.parseInt(newStr.trim());
      }catch(Exception e){
       e.printStackTrace();
       
      }
      DebugLog.d(TAG, "cNumber = "+cNumber);
      sumValues += cNumber;
      
      Log.e(TAG, "sumValues = "+sumValues);
     }
     
    }
    times++;
   }
   DebugLog.i(TAG, "process end");
  } catch (IOException e) {
   Log.e(TAG, "thread error");
   e.printStackTrace();
  } finally {
   if(in != null){
    try {
     in.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
    in = null;
   }
   if(process != null){
    DebugLog.d(TAG, "finally process != null");
    process.destroy();
    
   }
   
  }
  return sumValues / ALLOW_PING_TIMES;
 }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值