How to Ping External Ip from Java Android

How to ping an IP address

You can not simply ping in Java as it relies on ICMP, which is sadly not supported in Java

http://mindprod.com/jgloss/ping.html

Use sockets instead

Hope it helps

Ping an external IP in Kotlin

Considering that a ping is just an ICMP ECHO request, I think the easiest way to do that would just be to use InetAddress.isReachable().

According to the Javadoc:

public boolean isReachable (int timeout)

Test whether that address is reachable. Best effort is made by the
implementation to try to reach the host, but firewalls and server
configuration may block requests resulting in a unreachable status
while some specific ports may be accessible.

Android implementation attempts ICMP ECHO REQUESTs first, on failure
it will fall back to TCP ECHO REQUESTs. Success on either protocol
will return true.

Ping Application in Android

I have used following code to ping.

public String ping(String url) {
String str = "";
try {
Process process = Runtime.getRuntime().exec(
"/system/bin/ping -c 8 " + url);
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
int i;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((i = reader.read(buffer)) > 0)
output.append(buffer, 0, i);
reader.close();

// body.append(output.toString()+"\n");
str = output.toString();
// Log.d(TAG, str);
} catch (IOException e) {
// body.append("Error\n");
e.printStackTrace();
}
return str;
}

Here in the url, you need to pass the address, on which you want to ping.



Related Topics



Leave a reply



Submit