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()
Get a free local port.
ServerSocket ss;
int freePort = 0;
for (int i = 0; i < 10; i++) {
    freePort = (int) (10000 + Math.round(Math.random() * 10000));
    freePort = freePort % 2 == 0 ? freePort : freePort + 1;
    try {
        ss = new ServerSocket(freePort);
        freePort = ss.getLocalPort();
...
intgetFreePort()
Returns the free port on the local host.
int port = -1;
while (port <= 0) {
    Thread.sleep(100);
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(0);
        port = serverSocket.getLocalPort();
    } finally {
...
intgetFreePort(int port)
get Free Port
int limit = 65534;
while (port < limit) {
    try {
        new ServerSocket(port).close();
        return port;
    } catch (IOException e) {
        ++port;
return -1;
intgetFreePort(int pPort)
Find a free port, starting from pport
int port = 0;
ServerSocket s = null;
boolean found = false;
while (!found) {
    try {
        s = new ServerSocket(pPort);
        if (s.isBound()) {
            port = s.getLocalPort();
...
intgetFreePortNoSych()
Returns a free port
ServerSocket socket = new ServerSocket(0);
int port = socket.getLocalPort();
socket.close();
return port;
intgetFreePortNumber(int defaultPortNumber)
get Free Port Number
int port = defaultPortNumber;
ServerSocket socket = null;
try {
    socket = new ServerSocket(0);
    port = socket.getLocalPort();
} catch (IOException e) {
    System.out.println("IOException occured while creating socket." + " Use a default port number " + port);
} finally {
...
intgetFreeServerPort(int port)
Get a free server port that can be used.
int resultPort = 0;
ServerSocket s;
try {
    s = new ServerSocket(port);
    resultPort = s.getLocalPort();
    s.close();
} catch (IOException e) {
    resultPort = 0;
...
StringgetImportedXmlSchemaPath(String namespace, String portType, String operation)
get Imported Xml Schema Path
if (namespace == null || portType == null || operation == null) {
    throw new URISyntaxException(namespace + " " + portType + " " + operation,
            "The arguments can't be empty, please check");
StringBuilder builder = new StringBuilder(replaceAllLimited(new URI(namespace).getRawSchemeSpecificPart()));
if (builder.length() > 0) {
    while (builder.charAt(0) == '/') {
        builder.deleteCharAt(0);
...
intgetNextAvailable(int port)
get Next Available
int nextPort = port;
while (!isAvailable(nextPort)) {
    nextPort++;
return nextPort;
intgetNextAvailablePort()
get Next Available Port
for (int index = 0; index < 5; index++) {
    try {
        ServerSocket socket = new ServerSocket(0);
        int unusedPort = socket.getLocalPort();
        socket.close();
        if (isValidPort(unusedPort)) {
            return unusedPort;
    } catch (IOException e) {
        throw new IllegalStateException("Can not find an open network port", e);
throw new IllegalStateException("Can not find an open network port");