Necesito hacer un icmp ping a un host desde mi dispositivo Android. Necesito medir el tiempo de ida y vuelta. Soy competente con Android y Java, simplemente no sé qué biblioteca usar. ¿Cómo lo hago? ¿Es posible a través de 3G, Edge?cómo hacer icmp ping en android
Respuesta
Es probable que desee utilizar el isReachable
- ver más detalles en el Android doc. Sin embargo, aparentemente algunas redes bloquean ICMP. Hay una publicación donde puedes leer más sobre este tema here.
puede utilizar el código de fuente abierta de emulador de terminal disponibles here
construir la biblioteca (usando cygwin y android-ndk) archivo y luego usar
sí se puede hacer ping con 3G, EDGE, inalámbrico lo , siempre y cuando tengas conectividad. La única limitación es en el emulador, consulte aquí: http://groups.google.com/group/android-developers/browse_thread/thread/8657506be6819297
Aquí está mi función de 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;
}
}
}
ping (8.8.8.8) siempre falla – danarj
No funciona en samsung s3 4.2.2 y otros dispositivos 4.2.2 !! –
tenga cuidado con la coincidencia de cadena: .contains "100% de pérdida de paquete" y "0% de pérdida de paquete" son los mismos. –
- 1. Android ICMP ping
- 2. pitón no privilegiado ICMP
- 3. Cómo hacer ping a IP externa desde Java Android
- 4. ¿Cómo puedo hacer ping a un host con un Perl one-liner con Net :: Ping?
- 5. ¿Cómo implemento el ping ICMP en Ruby utilizando solo la biblioteca de socket estándar?
- 6. ¿Cómo hacer ping en JavaScript o jQuery?
- 7. Hacer ping en un rango de IP con Scapy
- 8. Servicio de Android - Ping URL
- 9. ¿Cómo hacer "ping" desde la aplicación Node.js?
- 10. Hacer ping en las instancias de EC2
- 11. ICMP y el iPhone SDK
- 12. ¿Qué protocolos se usan para PING?
- 13. Escuchar paquetes ICMP en C#
- 14. Cómo hacer ping a un huésped de ubuntu en VirtualBox
- 15. ¿Cómo acceder/hacer ping a un servidor ubicado en AWS?
- 16. Cómo ICMP y traceroutes en Java
- 17. Enchufes ICMP (linux)
- 18. Hacer ping en un nombre de host en la red
- 19. ¿Cómo hacer un verdadero ping de Java desde Windows?
- 20. Cómo hacer ping a un servicio web como svc, asmx
- 21. ¿Cómo es que ping no necesita privilegios administrativos en Windows?
- 22. ¿Por qué funciona el ping sin privilegios de administrador?
- 23. Usando ping en C#
- 24. Ping un sitio en ruby?
- 25. TraceRoute y Ping en C#
- 26. ¿alguna forma de "hacer ping" un número de teléfono?
- 27. ¿Cómo hacer notificaciones en android?
- 28. código para hacer ping a sitios web funciona a veces
- 29. ¿Debo hacer ping al servidor mysql antes de cada consulta?
- 30. Cómo escribir un método simple de Ping en Cocoa/Objective-C
¿Por qué necesita para medir el tiempo de ida y vuelta ICMP? – dbasnett