Java HTTP Port Find getAvailablePort()

Here you can find the source of getAvailablePort()

Description

get Available Port

License

Open Source License

Return

Port, available for local use

Declaration

public static int getAvailablePort() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.ServerSocket;

public class Main {
    /**//from w  ww  .j a v  a 2 s .com
     * @return Port, available for local use
     */
    public static int getAvailablePort() {
        try {
            try (final ServerSocket serverSocket = new ServerSocket(0)) {
                return serverSocket.getLocalPort();
            }
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}

Related

  1. getAvailablePort()
  2. getAvailablePort()
  3. getAvailablePort()
  4. getAvailablePort()
  5. getAvailablePort()
  6. getAvailablePort()
  7. getAvailablePort()
  8. getAvailablePort()
  9. getAvailablePort(final int basePort)