【问题标题】:Ping Clients of Android Mobile AP安卓手机APP Ping客户端
【发布时间】:2012-08-27 22:45:54
【问题描述】:

我想 ping 我的移动 AP 的客户端。这样我想看看客户端是否真的连接到我的热点,因为 /proc/net/arp 只有在我关闭热点时才会刷新。

这是我的异步任务:

    protected Boolean doInBackground(Object... arg0) {
    // TODO Auto-generated method stub
    try {
        InetAddress ip = (InetAddress) arg0[0];
        this.context = (Context) arg0[1];
        return connected =  ip.isReachable(5000);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}

@Override
protected void onPostExecute(Boolean result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    android.widget.Toast.makeText(this.context, String.valueOf(this.connected), android.widget.Toast.LENGTH_SHORT).show();

}

当您的设备未植根时,是否有办法 ping 客户端?

【问题讨论】:

  • 要记住的一点:即使发送 ping 没有问题,也不能保证您的客户会响应 ICMP...
  • 但总比没有好。;)

标签: android connection client ping access-point


【解决方案1】:

既然你有一个 IP 地址,你可以这样做这个

public static String ping(String _ip) {

    try {
        String command = "ping -c 10 " + _ip;
        Process p = null;
        p = Runtime.getRuntime().exec(command);
        int status = p.waitFor();
        InputStream input = p.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(input));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null) {
            buffer.append(line);
            buffer.append("\n");
        }
        String bufferStr = buffer.toString();
        return bufferStr;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多