Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 丟包率的計算

android 丟包率的計算

編輯:關於Android編程

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ss {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// System.out.println(iplongToIp(999756777));
		String lost = new String();
		String delay = new String();
		Process p = null;
		try {
			p = Runtime.getRuntime().exec("ping 192.168.146.55");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BufferedReader buf = new BufferedReader(new InputStreamReader(
				p.getInputStream()));
		String str = new String();
		try {
			while ((str = buf.readLine()) != null) {
				System.out.println(str);
				if (str.contains("packet loss")) {
					int i = str.indexOf("received");
					int j = str.indexOf("%");
					System.out.println("丟包率:" + 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);
					System.out.println("延遲:" + str.substring(i + 1, j));
					delay = str.substring(i + 1, j);
					delay = delay + "ms";
				}

			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		System.out.println(lost);
	}

	public static String iplongToIp(long ipaddress) {
		StringBuffer sb = new StringBuffer("");
		sb.append(String.valueOf((ipaddress >>> 24)));
		sb.append(".");
		sb.append(String.valueOf((ipaddress & 0x00FFFFFF) >>> 16));
		sb.append(".");
		sb.append(String.valueOf((ipaddress & 0x0000FFFF) >>> 8));
		sb.append(".");
		sb.append(String.valueOf((ipaddress & 0x000000FF)));
		return sb.toString();
	}
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved