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
String Replace Method Is Not Replacing Characters
Difference Between Jsf, Servlet and Jsp
How to Set Environment Variables from Java
Java Logical Operator Short-Circuiting
:: (Double Colon) Operator in Java 8
Sending Http Post Request in Java
How to Properly Stop the Thread in Java
Xss Prevention in Jsp/Servlet Web Application
Android, Getting Resource Id from String
How to Paginate Firestore With Android
Firestore Whereequalto, Orderby and Limit(1) Not Working
Firebase Ui Authentication With Google Fails With Message (Code:10 Message:10)
How to Get the Download Url from Firebase Storage
Can't Start Eclipse - Java Was Started But Returned Exit Code=13