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

intfindFreePort()
See https://gist.github.com/vorburger/3429822 Returns a free port number on localhost.
ServerSocket socket = null;
try {
    socket = new ServerSocket(0);
    socket.setReuseAddress(true);
    int port = socket.getLocalPort();
    try {
        socket.close();
    } catch (IOException e) {
...
intfindFreePort()
Find a free port for communication with Prolog.
ServerSocket ss = new ServerSocket(0);
int port = ss.getLocalPort();
ss.close();
return port;
intfindFreePort()
This is really only used for unit tests but is included in the library so it can be reused across modules.
ServerSocket server;
try {
    server = new ServerSocket(0);
    int port = server.getLocalPort();
    ourPorts.add(port);
    server.close();
    Thread.sleep(500);
    return port;
...
intfindFreePort()
find Free Port
ServerSocket socket = new ServerSocket(0);
try {
    return socket.getLocalPort();
} finally {
    try {
        socket.close();
    } catch (IOException e) {
ServerSocketfindFreePort()
find Free Port
ServerSocket server = null;
try {
    server = new ServerSocket(0, 1000, InetAddress.getLocalHost());
    server.setReuseAddress(true);
} catch (IOException e) {
    e.printStackTrace();
return server;
...
intfindFreePort()
Returns a free TCP/IP port number on localhost.
try (final ServerSocket socket = new ServerSocket(0)) {
    socket.setReuseAddress(true);
    return socket.getLocalPort();
} catch (final IOException ex) {
    throw new IllegalStateException("Could not find a free TCP/IP port", ex);
intfindFreePort()
Attempts to find a free port
return findFreePort(DEFAULT_PORT);
intfindFreePort()
Find free unused TCP port.
try (ServerSocket socket = new ServerSocket(0)) {
    return socket.getLocalPort();
} catch (IOException e) {
    return -1;
intfindFreePort(int start, int len)
Search for a free TCP and UDP port in the given range
int port = start;
int end = (len == 0) ? 65535 : start + len;
if (end > 65535)
    end = 65535;
while (port < end) {
    try {
        ServerSocket socket = new ServerSocket(port);
        socket.close();
...
intfindFreePort(int start, int limit)
find Free Port
if (start == 0) {
    return 0;
int found = 0;
int port = start;
int finish = start + limit;
while (found == 0 && port < finish) {
    if (isPortAvailable(port)) {
...