Java HTTP Port Find isAlive(String hostname, int port, int retries)

Here you can find the source of isAlive(String hostname, int port, int retries)

Description

is Alive

License

Open Source License

Declaration

private static boolean isAlive(String hostname, int port, int retries)
            throws Exception 

Method Source Code

//package com.java2s;
/*//from  w  w w.ja v  a 2  s  .c  o m
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

import java.io.IOException;

import java.net.Socket;

public class Main {
    public static int DEAULT_WAIT_TIME = 6000;

    private static boolean isAlive(String hostname, int port, int retries)
            throws Exception {
        int cnt = 0;
        System.out.println("Ping host:port " + hostname + ":" + port);
        while (cnt <= retries) {
            System.out.println("ping [#" + cnt + "] ...");

            boolean reachable = isReachableByPing(hostname, port);
            //   boolean reachable = isReachable(InetAddress.getByName(hostname).getHostAddress());
            if (reachable)
                return true;

            cnt++;
            // in cases where retries is set to 0 dont sleep
            if (cnt > retries)
                break;

            Thread.sleep(DEAULT_WAIT_TIME);

        }
        System.out.println("Could not ping host:port " + hostname + ":"
                + port);
        return false;
    }

    public static boolean isReachableByPing(String host, int port) {
        Socket ignored = null;
        try {
            ignored = new Socket(host, port);
            ignored.close();
            return true;
        } catch (IOException e) {
            return false;
        }

        //      try{
        //          // nc -zv <hostname/ip> <port/port range>
        //               String cmd = "";
        //               if(System.getProperty("os.name").startsWith("Windows")) {   
        //                       // For Windows
        //                       cmd = "nc -zv " + host + " " + port;
        //               } else {
        //                       // For Linux and OSX
        //                  cmd = "nc -zv " + host + " " + port;
        //               }
        //
        //               Process myProcess = Runtime.getRuntime().exec(cmd);
        //               myProcess.waitFor();
        //
        //               if(myProcess.exitValue() == 0) {
        //
        //                       return true;
        //               } 
        //
        //                return false;
        //               
        //
        //       } catch( Exception e ) {
        //
        //               e.printStackTrace();
        //               return false;
        //       }
    }
}

Related

  1. getValidZooKeeperPort()
  2. getWsdlLocation(String portPrefix)
  3. getXmlReport(String fileName)
  4. hasPermissionToBindPort(int port)
  5. isActivePort(int port)
  6. isAvailablePort(int port)
  7. isBindable(int port)
  8. isBindedPort(String host, int port)
  9. isBound(int port)