A simple java utility to check if the server alive

    /**
     * To check if a server is alive
     *
     * @param host either host name or host IP address
     * @param port
     * @return true or false
     */
    public static boolean isServerAlive(String host, int port) {
        java.net.Socket soc = null;

        try {
            java.net.InetAddress addr = java.net.InetAddress.getByName(host);
            soc = new java.net.Socket(addr, port);

            return true;
        } catch (java.io.IOException e) {
            return false;
        } finally {
            if (soc != null) {
                try {
                    soc.close();
                } catch (java.io.IOException e) {
                }
            }
        }
    }

Leave a Reply