当前位置:操作系统 > 安卓/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();
	}
}

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,