Android ICMP ping

时间:2012-01-30 10:05:47

标签: java android networking

有没有办法ping主机(标准Android或通过NDK实现),并获得有关响应的详细信息? (时间,ttl,丢包等..) 我在考虑一些具有此功能的开源应用程序,但找不到任何...

由于

2 个答案:

答案 0 :(得分:13)

Afaik,发送ICMP ECHO请求需要 root (即执行它需要setuid的应用程序) - 并且目前可能在“股票”Android(地狱) ,即使Android中的InetAddress#isReachable()方法是joke根据规范也不起作用。)

使用/ usr / bin / ping& amp;的一个非常基本的例子进程 - 使用AsyncTask读取ping结果:

public class PingActivity extends Activity {
    PingTask mTask;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTask = new PingTask();
        // Ping the host "android.com"
        mTask.execute("android.com");
    }

    @Override
    protected void onPause() {
        super.onPause();
        mTask.stop();
    }

    class PingTask extends AsyncTask<String, Void, Void> {
        PipedOutputStream mPOut;
        PipedInputStream mPIn;
        LineNumberReader mReader;
        Process mProcess;
        TextView mText = (TextView) findViewById(R.id.text);
        @Override
        protected void onPreExecute() {
            mPOut = new PipedOutputStream();
            try {
                mPIn = new PipedInputStream(mPOut);
                mReader = new LineNumberReader(new InputStreamReader(mPIn));
            } catch (IOException e) {
                cancel(true);
            }

        }

        public void stop() {
            Process p = mProcess;
            if (p != null) {
                p.destroy();
            }
            cancel(true);
        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                mProcess = new ProcessBuilder()
                    .command("/system/bin/ping", params[0])
                    .redirectErrorStream(true)
                    .start();

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

                    // in -> buffer -> mPOut -> mReader -> 1 line of ping information to parse
                    while ((count = in.read(buffer)) != -1) {
                        mPOut.write(buffer, 0, count);
                        publishProgress();
                    }
                    out.close();
                    in.close();
                    mPOut.close();
                    mPIn.close();
                } finally {
                    mProcess.destroy();
                    mProcess = null;
                }
            } catch (IOException e) {
            }
            return null;
        }
        @Override
        protected void onProgressUpdate(Void... values) {
            try {
                // Is a line ready to read from the "ping" command?
                while (mReader.ready()) {
                    // This just displays the output, you should typically parse it I guess.
                    mText.setText(mReader.readLine());
                }
            } catch (IOException t) {
            }
        }
    }
}

答案 1 :(得分:0)

我找到了一种在没有root的情况下执行ping命令的方法 首先生成'sh'进程,然后在该shell中执行'ping',代码为:

p = new ProcessBuilder("sh").redirectErrorStream(true).start();

DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("ping -c 10 " + host + '\n');
os.flush();

// Close the terminal
os.writeBytes("exit\n");
os.flush();

// read ping replys
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;

while ((line = reader.readLine()) != null) {
    System.out.println(line);
}

使用CyanogenMod 7.1.0(Android 2.3.7)在我的HTC设备上正常工作