Last active
February 13, 2017 10:07
-
-
Save brownsoo/4a4484c4069c7da28115b5ce61511dc7 to your computer and use it in GitHub Desktop.
Check Reachability using ping in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 주의사항 http://stackoverflow.com/a/10145643 | |
// The isReachable failure is an outstanding issue. | |
// Again - there are several online resources indicating that there is no "perfect" way of doing this at the time of this writing, | |
// due to the way the JVM tries to reach hosts - I guess it is an intrinsically platform specific task which, although simple, hasn't yet been abstracted sufficiently by the JVM. | |
private boolean ping(String host) { | |
Runtime runTime = Runtime.getRuntime(); | |
String host = host; | |
//-c CNT Send only CNT pings | |
//-W timeout Seconds | |
String cmd = "ping -c 1 -W 10 "+host; | |
Process process = null; | |
try { | |
process = runTime.exec(cmd); | |
} catch(IOException ie){ | |
Log.e(TAG, "runtime.exec()", ie); | |
} | |
try { | |
if (process != null) { | |
process.waitFor(); | |
// 0 : success, 1 : fail, 2 : error | |
int result = process.exitValue(); | |
return result == 0; | |
} | |
} catch(InterruptedException ie){ | |
Log.e(TAG, "proc.waitFor", ie); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment