Java HTTP Port Find isHostAvailable(final String host, final int port)

Here you can find the source of isHostAvailable(final String host, final int port)

Description

Returns whether the given host is accepting connections on the given port.

License

Open Source License

Parameter

Parameter Description
host the given host
port the given port

Return

whether the given host is accepting connections on the given port

Declaration

public static boolean isHostAvailable(final String host, final int port) 

Method Source Code

//package com.java2s;
/*/*from   w ww  .  j  a v  a  2  s  . com*/
 * NetworkUtils.java
 *
 * Created on Jan 12, 2010, 9:17:04 AM
 *
 * Description: NetworkUtils utilities.
 *
 * Copyright (C) Jan 12, 2010 reed.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.io.IOException;

import java.net.InetSocketAddress;

import java.net.Socket;

public class Main {
    /** the upper bound of dynamic TCP ports */
    static final int UPPER_PORT_BOUND = 65535;
    /** the socket connection timeout */
    public static final int CONNECTION_TIMEOUT = 10000;

    /** Returns whether the given host is accepting connections on the given port.
     *
     * @param host the given host
     * @param port the given port
     * @return whether the given host is accepting connections on the given port
     */
    public static boolean isHostAvailable(final String host, final int port) {
        //Preconditions
        assert host != null : "the host must not be null";
        assert !host.isEmpty() : "the host must not be empty";
        assert port >= 0 && port <= UPPER_PORT_BOUND : "the port be within the range 0 - 65535";

        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(host, port), CONNECTION_TIMEOUT);
        } catch (IOException ex) {

            try {
                socket.close();
            } catch (IOException ex1) {
            }
            return false;
        }
        try {
            socket.close();
        } catch (IOException ex1) {
            return false;
        }
        return true;
    }
}

Related

  1. isBound(int port)
  2. isBound(int port)
  3. isConnectionAlive(String protocol, String host, int port)
  4. isFreePort(int portNumber)
  5. isFreeTCPPort(final int port)
  6. isHostReachable(String host, int port, int connTimeout)
  7. isListening(String host, int port)
  8. isLivereloadAvailable(int port)
  9. isLocalPortAvailable(final int port)