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
int port = -1;
try {
    ServerSocket server = new ServerSocket(0);
    port = server.getLocalPort();
    server.close();
} catch (IOException exception) {
    port = -1;
return port;
intgetAvailablePort()
Getting a available port on range 60000:61000
int initialP = 60000;
int finalP = 61000;
for (int i = initialP; i < finalP; i++) {
    int port = new Random().nextInt(finalP - initialP) + initialP;
    ServerSocket ss = null;
    DatagramSocket ds = null;
    try {
        ss = new ServerSocket(port);
...
intgetAvailablePort()
get Available Port
ServerSocket ss = null;
try {
    ss = new ServerSocket();
    ss.bind(null);
    return ss.getLocalPort();
} catch (IOException e) {
    return getRandomPort();
} finally {
...
intgetAvailablePort()
get Available Port
ServerSocket ss = null;
try {
    ss = new ServerSocket();
    ss.bind(null);
    return ss.getLocalPort();
} catch (IOException e) {
    return getRandomPort();
} finally {
...
intgetAvailablePort()
Find a port that is available.
try (ServerSocket socket = new ServerSocket(0)) {
    return socket.getLocalPort();
} catch (IOException e) {
    throw new IllegalStateException("Cannot find available port: " + e.getMessage(), e);
intgetAvailablePort()
get Available Port
ServerSocket serverside = null;
int port = 0;
try {
    serverside = new ServerSocket(0);
    port = serverside.getLocalPort();
} catch (IOException exception) {
    exception.printStackTrace();
} finally {
...
intgetAvailablePort()
Returns port number which is currently not listened by local services.
try (final ServerSocket ss = new ServerSocket(0)) {
    return ss.getLocalPort();
} catch (final IOException e) {
    throw new IllegalStateException("Unable to get local port: no ports available");
intgetAvailablePort()
get Available Port
try {
    try (final ServerSocket serverSocket = new ServerSocket(0)) {
        return serverSocket.getLocalPort();
} catch (IOException e) {
    throw new IllegalStateException(e);
intgetAvailablePort()
get Available Port
for (int i = 0; i < 50; i++) {
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(0);
        int port = serverSocket.getLocalPort();
        if (port != 0) {
            return port;
    } finally {
        serverSocket.close();
throw new IOException("could not find free port");
intgetAvailablePort()
get one available port
return availablePort(0);