Java HTTP Port Find getNextAvailablePort(int port)

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

Description

get Next Available Port

License

Open Source License

Declaration

public static int getNextAvailablePort(int port) 

Method Source Code


//package com.java2s;
import java.io.IOException;
import java.net.Socket;

public class Main {
    public static int getNextAvailablePort(int port) {
        int offset = detectPortOffset(port);
        if (offset < 0) {
            //FIXME that's really an error
            return port;
        }// www  .  j a  va2  s. c o  m
        return port + offset;
    }

    public static int detectPortOffset(int port) {
        for (int offset = 0; offset < 100; offset++) {
            int newPort = port + offset;
            try (Socket socket = new Socket("localhost", newPort)) {
            } catch (IOException ignored) {
                return offset;
            }
        }
        return -1;//TODO handle error?
    }
}

Related

  1. getFreeServerPort(int port)
  2. getImportedXmlSchemaPath(String namespace, String portType, String operation)
  3. getNextAvailable(int port)
  4. getNextAvailablePort()
  5. getNextAvailablePort(final int min, final int max, final Collection excepted)
  6. getNodeName(final String managementServerHostName, final int managementPort)
  7. getNonPrivilegedPort()
  8. getOpenPort()
  9. getPort(final int suggestedPort)