Java HTTP Port Find getAvailablePort(int defaultPort)

Here you can find the source of getAvailablePort(int defaultPort)

Description

get Available Port

License

Open Source License

Declaration

public static int getAvailablePort(int defaultPort) throws IOException 

Method Source Code

//package com.java2s;

import java.io.IOException;

import java.net.ServerSocket;

public class Main {
    public static int getAvailablePort(int defaultPort) throws IOException {

        try {//from  ww w .j  a v  a 2  s .com
            ServerSocket ss = new ServerSocket(defaultPort);
            ss.close();
            return defaultPort;
        } catch (IOException e) {
        }
        ServerSocket ss = new ServerSocket(0);
        int port = ss.getLocalPort();
        ss.close();
        return port;
    }
}

Related

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