在Android中执行ping:无法预料的延迟

时间:2014-10-13 20:48:29

标签: android ping processbuilder

我用这段代码开始长ping:

PipedOutputStream pOut;
PipedInputStream pIn;
LineNumberReader reader;

pOut = new PipedOutputStream();
try {
    pIn = new PipedInputStream(pOut);
    reader = new LineNumberReader(new InputStreamReader(pIn));
} catch (IOException e) {
    return;
}

try {
    mProcess = new ProcessBuilder()
        .command("/system/bin/ping", "-w " + 900, "-i " + 1, "google.com")
        .redirectErrorStream(true)
        .start();

    try {
        InputStream in = mProcess.getInputStream();
        byte[] buffer = new byte[1024];
        int count;

        while ((count = in.read(buffer)) != -1) {
            pOut.write(buffer, 0, count);

            try {
                while (reader.ready()) {
                    String line = reader.readLine().trim();
                    log(LogTags.TEST, "Ping line: %s", line);
                    …
                }
            } catch (IOException e) {
                …
            }
        }
        in.close();
        pOut.close();
        pIn.close();
    } finally {
        mProcess = null;
    }

} catch (IOException ignored) {
}

它有效,但有时我看到ping之间的时间间隔太大了:

10-14 00:23:21.404  zzz﹕ Ping line: 64 bytes from 64.233.161.113: icmp_seq=702 ttl=46 time=7.78 ms
10-14 00:23:47.394  zzz﹕ Ping line: 64 bytes from 64.233.161.113: icmp_seq=703 ttl=46 time=17.5 ms
10-14 00:24:14.480  zzz﹕ Ping line: 64 bytes from 64.233.161.113: icmp_seq=704 ttl=46 time=15.0 ms
10-14 00:24:24.404  zzz﹕ Ping line: 64 bytes from 64.233.161.113: icmp_seq=705 ttl=46 time=9.02 ms
10-14 00:24:58.394  zzz﹕ Ping line: 64 bytes from 64.233.161.113: icmp_seq=706 ttl=46 time=20.2 ms
10-14 00:24:59.394  zzz﹕ Ping line: 64 bytes from 64.233.161.113: icmp_seq=707 ttl=46 time=20.9 ms
10-14 00:25:00.395  zzz﹕ Ping line: 64 bytes from 64.233.161.113: icmp_seq=708 ttl=46 time=16.2 ms
10-14 00:25:06.574  zzz﹕ Ping line: 64 bytes from 64.233.161.113: icmp_seq=709 ttl=46 time=14.6 ms

我在命令中指定了间隔1秒。为什么我看到10个或更多?如何解决?

0 个答案:

没有答案