怎么在android上ping接口,如何在Android上的icmp ping

是的,只要你有连接,你可以ping 3G,边缘,无线.唯一的限制是在仿真器中,参见这里:

http://groups.google.com/group/android-developers/browse_thread/thread/8657506be6819297

这是我的ping功能:

package com.namespace.router.api;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import android.util.Log;

public class Network {

private static final String TAG = "Network.java";

public static String pingError = null;

/**

* Ping a host and return an int value of 0 or 1 or 2 0=success, 1=fail, 2=error

*

* Does not work in Android emulator and also delay by '1' second if host not pingable

* In the Android emulator only ping to 127.0.0.1 works

*

* @param String host in dotted IP address format

* @return

* @throws IOException

* @throws InterruptedException

*/

public static int pingHost(String host) throws IOException, InterruptedException {

Runtime runtime = Runtime.getRuntime();

Process proc = runtime.exec("ping -c 1 " + host);

proc.waitFor();

int exit = proc.exitValue();

return exit;

}

public static String ping(String host) throws IOException, InterruptedException {

StringBuffer echo = new StringBuffer();

Runtime runtime = Runtime.getRuntime();

Log.v(TAG, "About to ping using runtime.exec");

Process proc = runtime.exec("ping -c 1 " + host);

proc.waitFor();

int exit = proc.exitValue();

if (exit == 0) {

InputStreamReader reader = new InputStreamReader(proc.getInputStream());

BufferedReader buffer = new BufferedReader(reader);

String line = "";

while ((line = buffer.readLine()) != null) {

echo.append(line + "\n");

}

return getPingStats(echo.toString());

} else if (exit == 1) {

pingError = "failed, exit = 1";

return null;

} else {

pingError = "error, exit = 2";

return null;

}

}

/**

* getPingStats interprets the text result of a Linux ping command

*

* Set pingError on error and return null

*

* http://en.wikipedia.org/wiki/Ping

*

* PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

* 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.251 ms

* 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.294 ms

* 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.295 ms

* 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.300 ms

*

* --- 127.0.0.1 ping statistics ---

* 4 packets transmitted, 4 received, 0% packet loss, time 0ms

* rtt min/avg/max/mdev = 0.251/0.285/0.300/0.019 ms

*

* PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.

*

* --- 192.168.0.2 ping statistics ---

* 1 packets transmitted, 0 received, 100% packet loss, time 0ms

*

* # ping 321321.

* ping: unknown host 321321.

*

* 1. Check if output contains 0% packet loss : Branch to success -> Get stats

* 2. Check if output contains 100% packet loss : Branch to fail -> No stats

* 3. Check if output contains 25% packet loss : Branch to partial success -> Get stats

* 4. Check if output contains "unknown host"

*

* @param s

*/

public static String getPingStats(String s) {

if (s.contains("0% packet loss")) {

int start = s.indexOf("/mdev = ");

int end = s.indexOf(" ms\n", start);

s = s.substring(start + 8, end);

String stats[] = s.split("/");

return stats[2];

} else if (s.contains("100% packet loss")) {

pingError = "100% packet loss";

return null;

} else if (s.contains("% packet loss")) {

pingError = "partial packet loss";

return null;

} else if (s.contains("unknown host")) {

pingError = "unknown host";

return null;

} else {

pingError = "unknown error in getPingStats";

return null;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,可以使用ping命令来进行ICMP丢包测试。ping命令可以通过发送ICMP回声请求消息给目的地,并报告是否收到所希望的ICMP回声应答,从而检查网络是否通畅或者网络连接速度。具体参数可以定义包的个数、包的最大存活时间等。\[1\] 例如,使用ping -c4 192.168.1.118命令可以发送4个数据包到目标IP地址,并返回丢包率及平均时间等统计信息。\[2\] 在Android中,可以通过将ping命令复制到私有文件目录下,然后执行该命令来进行ping测试。可以使用以下代码将assets目录下的ping文件复制到私有文件目录下的ping文件中: ```java private boolean cpPingLib(){ String path = TestPing.this.getApplicationContext().getFilesDir().getAbsolutePath()+ "/ping"; File file = new File(path); if(file.exists()){ return true; } FileOutputStream out = null; InputStream in = null ; try { in = TestPing.this.getAssets().open("ping"); out = new FileOutputStream(file); int length = -1; byte\[\] buf = new byte\[1024\]; while ((length = in.read(buf)) != -1){ out.write(buf, 0, length); } } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; }finally{ try { out.flush(); in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } return true; } ``` 通过调用cpPingLib()方法,可以将ping命令复制到私有文件目录下的ping文件中。然后可以在Android应用程序中执行该ping命令来进行ICMP丢包测试。 #### 引用[.reference_title] - *1* [Android基础篇_ping实现](https://blog.csdn.net/baidu_30084597/article/details/79603367)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [androidping的实现丢包率的获取](https://blog.csdn.net/zyp009/article/details/18184555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值