Java HTTP Port Find getAvailablePort(int port)

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

Description

get Available Port

License

Open Source License

Declaration

public static Integer getAvailablePort(int port) 

Method Source Code

//package com.java2s;

import java.net.ServerSocket;

public class Main {
    public static Integer getAvailablePort(int port) {
        ServerSocket socket = null;
        for (int i = 0; i < 2048; i++) {
            try {
                socket = new ServerSocket(port + i);
            } catch (Exception e) {

            } finally {
                if (null != socket) {
                    try {
                        socket.close();/*from   w w  w  .j  a  v  a2s  .co m*/
                        return port + i;
                    } catch (Exception e2) {

                    }
                }
            }
        }

        return null;
    }
}

Related

  1. getAvailablePort()
  2. getAvailablePort()
  3. getAvailablePort(final int basePort)
  4. getAvailablePort(int defaultPort)
  5. getAvailablePort(int port)
  6. getAvailablePort(int prefferedPort)
  7. getAvailablePort(int randomAttempts)
  8. getAvailablePort(int startPort)
  9. getAvailablePort(String hostname, String portRange)