Ho bisogno di fare un icmp ping ad un host dal mio dispositivo Android. Devo misurare il tempo di andata e ritorno. Sono abile con Android e Java, ma non so quale libreria usare. Come faccio? È possibile tramite 3G, Edge?come icmp ping su Android
risposta
Probabilmente si vorrà utilizzare lo isReachable
- vedere ulteriori dettagli nello Android doc. Tuttavia, apparentemente alcune reti bloccano l'ICMP. C'è un post in cui puoi leggere di più su questo numero here.
è possibile utilizzare il codice open source di emulatore di terminale disponibili here
costruire il file di libreria (utilizzando cygwin e android-ndk) e quindi utilizzare
Sì, è possibile eseguire il ping con 3G, EDGE, wireless qualunque , a patto che tu abbia la connettività. L'unica limitazione è nell'emulatore, vedere qui: http://groups.google.com/group/android-developers/browse_thread/thread/8657506be6819297
Ecco la mia funzione 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) fallisce sempre – danarj
Non funziona su samsung s3 4.2.2 e altro dispositivo 4.2.2 !! –
fare attenzione con la corrispondenza delle stringhe: .contains "100% perdita di pacchetti" e "0% perdita di pacchetti" sono gli stessi. –
- 1. Ping ICMP Android
- 2. Come abilitare Ping (ICMP) su Azure
- 3. python non privilegiato ICMP
- 4. Come implementare il ping ICMP in Ruby usando solo lo standard della libreria socket?
- 5. Servizio Android - Ping URL
- 6. ICMP e iPhone SDK
- 7. Che cos'è un ping SNMP?
- 8. È possibile eseguire il ping di PHP su un sistema remoto senza ICMP e senza programmi esterni?
- 9. Come posso eseguire il ping di un host con una linea singola Perl con Net :: Ping?
- 10. Come ping tramite Sockets Biblioteca - C
- 11. È possibile implementare ping su Windows Phone 7?
- 12. Come eseguire il ping su Ubuntu Guest su VirtualBox
- 13. Connettori ICMP (linux)
- 14. Ping di un intervallo IP con Scapy
- 15. Qual è il processo corretto per la richiesta/risposta echo ICMP su destinazioni non raggiungibili?
- 16. Funzione tipo ping C++ senza accesso superutente
- 17. Come mai ping non ha bisogno di privilegi amministrativi su Windows?
- 18. Come eseguire il ping del server in Android?
- 19. Differenza tra Ping su Windows e Ubuntu
- 20. Linux ping trasmissione interruttore
- 21. pitone con ping non privilegiato in linux IPPROTO_ICMP
- 22. Ascolta i pacchetti ICMP in C#
- 23. Latenza Ping Bluetooth
- 24. messaggio di errore irraggiungibile porta ICMP
- 25. C# ping minecraft
- 26. Come eseguire il ping da Flex - AIR?
- 27. Ping con iPhone SDK
- 28. Timeout ping predefinito
- 29. come utilizzare il ping in uno script
- 30. Come eseguire il "ping" da un'app Node.js?
Perché avete bisogno di misurare il tempo di andata e ritorno ICMP? – dbasnett