Java Utililty Methods Ping

List of utility methods to do Ping

Description

The list of methods to do Ping are organized into topic(s).

Method

booleanping(String ip)
ping
try {
    Process p = Runtime.getRuntime().exec("ping " + ip + " 1");
    return (p.waitFor() == 0);
} catch (IOException e) {
    return false;
} catch (InterruptedException e) {
    return false;
intpingHost(String host)
ping Host
try {
    String cmd = "";
    if (System.getProperty("os.name").startsWith("Windows")) {
        cmd = "cmd /c ping " + host + " -n 1";
    } else {
        cmd = "ping -c 1 " + host;
    Process myProcess = Runtime.getRuntime().exec(cmd);
...
booleanping(String url, int timeout)
Found on http://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-a-http-url-for-availability Pings a HTTP URL.
try {
    URLConnection conn = new URL(url).openConnection();
    if (conn instanceof FileURLConnection) {
        return true;
    HttpURLConnection connection = (HttpURLConnection) conn;
    connection.setConnectTimeout(timeout);
    connection.setReadTimeout(timeout);
...
booleanping(String url, int timeout)
Pings a HTTP URL.
try {
    HttpURLConnection connection = (HttpURLConnection) prepareURLConnection(url, timeout);
    connection.setRequestMethod("HEAD");
    int responseCode = connection.getResponseCode();
    return (200 <= responseCode && responseCode <= 399);
} catch (IOException exception) {
    return false;
booleanpingHttp(URL url, int retry, int timeout)
Pings an URL by using HttpURLConnection#getResponseCode() .
boolean success = false;
for (int i = 0; i < retry && !success; i++) {
    try {
        URLConnection conn = url.openConnection();
        if (conn instanceof HttpURLConnection) {
            int responseCode = ((HttpURLConnection) conn).getResponseCode();
            success = 200 == responseCode;
        } else {
...
booleanpingUrl(final String address)
ping Url
try {
    final URL url = new URL("http://" + address);
    final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    urlConn.setConnectTimeout(3000);
    final long startTime = System.currentTimeMillis();
    urlConn.connect();
    final long endTime = System.currentTimeMillis();
    if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
...
booleanpingURL(final String url)
ping URL
URLConnection conn = null;
try {
    URL u = new URL(url);
    conn = u.openConnection();
    conn.setReadTimeout((int) TimeUnit.SECONDS.toMillis(15));
    conn.setAllowUserInteraction(false);
    conn.connect();
    return true;
...
voidpingUrl(String Url)
ping Url
try {
    URL url = new URL(Url);
    HttpURLConnection con = (HttpURLConnection) (url.openConnection());
    System.setProperty("http.agent", "");
    con.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30");
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String str;
...