Java Utililty Methods HTTP Port Find

List of utility methods to do HTTP Port Find

Description

The list of methods to do HTTP Port Find are organized into topic(s).

Method

intgetAvailablePort()
get Available Port
ServerSocket ss = null;
try {
    ss = new ServerSocket();
    ss.bind(null);
    return ss.getLocalPort();
} catch (IOException e) {
    throw new IllegalStateException("", e);
} finally {
...
intgetAvailablePort(final int basePort)
get Available Port
if (basePort == 0) {
    return getAvailablePort();
if (basePort < 0 || basePort > 65535) {
    throw new IllegalArgumentException("Illegal port number: " + basePort);
for (int i = basePort; i <= 65535; i++) {
    if (isPortAvailable(i)) {
...
intgetAvailablePort(int defaultPort)
get Available Port
try {
    ServerSocket ss = new ServerSocket(defaultPort);
    ss.close();
    return defaultPort;
} catch (IOException e) {
ServerSocket ss = new ServerSocket(0);
int port = ss.getLocalPort();
...
intgetAvailablePort(int port)
get Available Port
int result = port;
while (true) {
    if (isPortAvailable(result)) {
        break;
    result++;
return result;
...
IntegergetAvailablePort(int port)
get Available Port
ServerSocket socket = null;
for (int i = 0; i < 2048; i++) {
    try {
        socket = new ServerSocket(port + i);
    } catch (Exception e) {
    } finally {
        if (null != socket) {
            try {
...
intgetAvailablePort(int prefferedPort)
get Available Port
int localPort = -1;
try (ServerSocket socket = new ServerSocket(prefferedPort)) {
    localPort = socket.getLocalPort();
} catch (IOException exp) {
    if (prefferedPort > 0) {
        return getAvailablePort(0);
return localPort;
intgetAvailablePort(int randomAttempts)
Attempts to find a free port between the MIN_PORT_NUMBER(9000) and MAX_PORT_NUMBER(11000).
ArrayList<Integer> failedPorts = new ArrayList<Integer>(randomAttempts);
Random randomNum = new Random();
int randomPort = MAX_PORT_NUMBER;
while (randomAttempts > 0) {
    randomPort = randomNum.nextInt(MAX_PORT_NUMBER - MIN_PORT_NUMBER) + MIN_PORT_NUMBER;
    if (checkIfPortAvailable(randomPort)) {
        return randomPort;
    failedPorts.add(randomPort);
    randomAttempts--;
randomPort = MAX_PORT_NUMBER;
while (true) {
    if (!failedPorts.contains(randomPort) && checkIfPortAvailable(randomPort)) {
        return randomPort;
    randomPort--;
intgetAvailablePort(int startPort)
Find the first available port after a given port
int port = startPort;
while (_assignedPorts.contains(port) && !portAvailable(port) && port < 0x7FFFFFFF) {
    port++;
if (0x7FFFFFFF == port) {
    throw new RuntimeException("no available port found starting at " + startPort);
_assignedPorts.add(port);
...
intgetAvailablePort(String hostname, String portRange)
get Available Port
StringTokenizer st = new StringTokenizer(portRange, ",");
while (st.hasMoreTokens()) {
    int index = portRange.indexOf('-');
    if (index == -1) {
        int port = Integer.parseInt(st.nextToken().trim());
        if (port == 0) {
            return 0;
        try {
            return getPort(port);
        } catch (IOException e) {
    } else {
        int startPort = Integer.parseInt(portRange.substring(0, index));
        int endPort = Integer.parseInt(portRange.substring(index + 1));
        if (endPort < startPort) {
            throw new IllegalArgumentException(
                    "Start port [" + startPort + "] must be greater than end port [" + endPort + "]");
        for (int i = startPort; i <= endPort; i++) {
            try {
                return getPort(i);
            } catch (IOException ex) {
throw new IOException("Failed to get free port from [" + portRange + "]");
ListgetAvailablePorts(int basePort, int portCount)
get Available Ports
List<Integer> availablePorts = new ArrayList<Integer>();
int port = basePort;
for (int i = 0; i < portCount; i++) {
    while (!isPortAvailable(port)) {
        port++;
    availablePorts.add(port++);
return availablePorts;