Java HTTP Port Find isPortAvailable(int port)

Here you can find the source of isPortAvailable(int port)

Description

Checks to see if a specific port is available.

License

Open Source License

Parameter

Parameter Description
port the port to check for availability

Declaration

public static boolean isPortAvailable(int port) 

Method Source Code

//package com.java2s;
/**//from www. j  a v  a2s .  c o  m
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

import java.io.IOException;

import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {
    /**
     * The minimum number of server port number.
     */
    public static final int MIN_PORT_NUMBER = 1;
    /**
     * The maximum number of server port number.
     */
    public static final int MAX_PORT_NUMBER = 49151;

    /**
     * Checks to see if a specific port is available.
     * 
     * @param port the port to check for availability
     */
    public static boolean isPortAvailable(int port) {

        if ((port < MIN_PORT_NUMBER) || (port > MAX_PORT_NUMBER))
            return false;

        ServerSocket ss = null;
        DatagramSocket ds = null;
        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);

            try {
                closeConnections(ss, ds);

                //Checking if port is open by trying to connect as a client;
                Socket socket = new Socket("127.0.0.1", port);
                socket.close();
                return false; //Someone responding on port - so not available;
            } catch (Exception e) {
                //Connection refused, so port must be available
            }

            return true;
        } catch (IOException e) {
        } finally {
            closeConnections(ss, ds);
        }

        return false;
    }

    private static void closeConnections(ServerSocket ss, DatagramSocket ds) {
        if (ds != null)
            ds.close();

        if (ss != null) {
            try {
                ss.close();
            } catch (IOException e) {
            }
        }
    }
}

Related

  1. isMulticastSupported(NetworkInterface pNif)
  2. isOpen(final int port)
  3. isPortActive(String host, int port)
  4. isPortAvailable(final int port)
  5. isPortAvailable(int p)
  6. isPortAvailable(int port)
  7. isPortAvailable(int port)
  8. isPortAvailable(int port)
  9. isPortAvailable(int port)