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

intgetFreePort()
Returns the next available port number on the local host.
ServerSocket socket = null;
try {
    socket = new ServerSocket(0);
    return socket.getLocalPort();
} catch (IOException e) {
} finally {
    if (socket != null) {
        try {
...
intgetFreePort()
Get available port.
try (ServerSocket socket = new ServerSocket(0)) {
    return socket.getLocalPort();
} catch (IOException ioe) {
    return -1;
intgetFreePort()
get Free Port
int result;
try {
    ServerSocket socket = new ServerSocket(0);
    result = socket.getLocalPort();
    socket.close();
} catch (IOException ioe) {
    throw new RuntimeException("Failed to find free port.", ioe);
return result;
intgetFreePort()
get Free Port
try {
    try (ServerSocket socket = new ServerSocket(0)) {
        socket.setReuseAddress(true);
        return socket.getLocalPort();
} catch (IOException e) {
    return -1;
intgetFreePort()
Get an arbitrary free port on the localhost
for (int port = 22332; port < 22500; port++) {
    if (trySocket(port)) {
        return port;
throw new IllegalStateException("Cannot find a single free port");
intgetFreePort()
get Free Port
ServerSocket serverSocket = new ServerSocket(0);
int port = serverSocket.getLocalPort();
serverSocket.close();
return port;
intgetFreePort()
get Free Port
try (ServerSocket s = new ServerSocket(0)) {
    s.setReuseAddress(true);
    return s.getLocalPort();
intgetFreePort()
get Free Port
try {
    final ServerSocket ss = new ServerSocket(0);
    ss.setReuseAddress(true);
    final int port = ss.getLocalPort();
    ss.close();
    return port;
} catch (final Throwable t) {
    throw new RuntimeException(t);
...
intgetFreePort()
tries to get a free port and returns it
ServerSocket serverSocket = null;
try {
    serverSocket = new ServerSocket(0);
    int freePort = serverSocket.getLocalPort();
    return freePort;
} catch (IOException e) {
    return -1;
} finally {
...
intgetFreePort()
Gets a random free port in the non-privileged range of 1025-65535.
return getFreePort(MIN_SAFE_PORT, MAX_PORT);