- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
有没有办法 ping 主机(标准 Android 或通过 NDK 实现),并获取有关响应的详细信息? (时间、ttl、丢失的包裹等。)我正在考虑一些具有此功能但找不到的开源应用程序......
谢谢
最佳答案
Afaik,发送 ICMP ECHO 请求需要 root(即需要 setuid 的应用程序)——而这在“stock”Android 中目前是不可能的(该死的) ,甚至 Android 中的 InetAddress#isReachable() 方法也是一个 joke,根据规范不工作)。
一个使用/usr/bin/ping & Process 的非常基本的示例 - 使用 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) {
}
}
}
}
关于java - Android ICMP ping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9062182/
我们有一个客户想要通过他们的 CRM 数据库并以某种方式确定有效的电话号码,而实际上没有人坐在那里尝试给他们打电话。 有没有办法在电话号码(包括固定电话)上做类似于“ping”的事情? 最佳答案 您将
我知道 ICMP 请求包含 IP 地址。客户端 MAC 地址是否包含在 ICMP 请求中? ping 请求中包含哪些其他信息(如果有)? 最佳答案 ICMP“ping”数据包,正式称为“echo re
据我所知,Ping 命令使用 ICMP 请求 那么是否可以直接从命令行发送带有 ping 命令的短文本? 最佳答案 怎么样ping -p pattern ?请记住,并非所有版本的 ping支持 -p选
我有一个需要持续网络监控的 Android 应用程序,我需要在有互联网和没有互联网时收到通知。我试过 Android 连接管理器,它只告诉互联网 wifi 是否已连接,但不告诉是否有可达性。所以我
我需要检查延迟的服务器拒绝 PING 请求,是否有另一种方法来检查我到服务器的延迟?提前致谢。 最佳答案 使用基于 TCP 的 ping。 如果您可以访问 Windows 框,请使用 http://t
我在 Linux 系统上不允许使用“ping”应用程序(ping:icmp 打开套接字:不允许操作)。但是,我正在编写的脚本(PHP,但如果需要,我可以对任何脚本/程序使用 exec() 调用)需要确
我想创建将通过 ping IP 列表进行验证的 bash 脚本 问题是尽管我将 ping 定义如下,但 ping 到任何地址都需要几秒钟(以防没有 ping 应答): Ping –c 1 126.7
最近几周我做了相当多的研究试图创建一个连接诊断工具,我不太想只检查连接是否可用而是诊断是否存在抖动、数据包丢失等.. 到目前为止,Java 似乎不支持真正的 ICMP 请求,并且有一些解决方法,但没有
我有一个域名要测试。 Ping 约为 20 毫秒。 “HTTP HEAD”约为 500 毫秒。 为什么他们之间有这么大的区别?这是服务器端的问题吗?是不是差别太大了? 25 次。 最佳答案 好吧,首先
我想在 ping 主机后获取 ping 执行时间和结果字符串。我该怎么做? 最佳答案 long currentTime = System.currentTimeMillis(); boolean is
我有以下代码: $Servers = "8.8.8.8" $TimeStart = Get-Date $TimeEnd = $TimeStart.AddMinutes(1) Do { Fore
例如,我输入 ping www.domain.com,我想要的输出如下: 64 bytes from yyy.xxx.com (www.domain.com): icmp_seq=32 ttl=52
在某些设备上,二进制 /system/bin/ping 似乎已被删除。当我通过 USB 连接设备并输入 adb shell ping 它说: /system/bin/sh: ping: not fou
您好,我需要使用 Java 代码执行 PING 命令并获取 ping 主机的摘要。如何用 Java 实现? 最佳答案 按照 viralpatel 的规定,您可以使用 Runtime.exec() 下面
我正在学习 C 中的 ping 实现。问题是,我使用原始套接字来接收数据包。对于所有数据包,我们在 ICMP header 中都有一个标识值。 我在多个终端运行ping。 例如,我在三个终端中运行三个
例如在Windows 7下命令的输出 ping -n 1 ::1 正在关注: Pinging ::1 with 32 bytes of data: Reply from ::1: time<1ms P
我正在制作一个 bash 脚本来配置我们使用的一些设备,但我试图在其中进行登录,换句话说.. 当脚本开始检查日期、时间、用户和其他值,然后 > 这个值到远程服务器中的 csv。 我需要 ping 来检
概念上的区别是什么?是的,我意识到它们都应该产生相似的结果,但我认为环回的想法是它实际上并没有到达堆栈的传输步骤。如果那是真的那么为什么 ping 你自己的 ip 工作得那么快? ping 您自己的地
我正在尝试连接到 Java 中的 URL,看看它们是否有效,我想知道是否需要连接到 HTTPS(端口 443?)或者仅连接到 HTTP(端口 80)就足够了。 连接到 HTTPS 网站是否有效?我应该
我正在尝试 ping 从 192.168.1.1 到 192.168.1.254 的 IP 地址。首先,我使用 InetAddress 类,但它被窃听了,有些 IP 即使可以访问也无法访问。之后我尝试
我是一名优秀的程序员,十分优秀!