List of utility methods to do HTTP Port Find
int | getAvailablePort() get Available Port ServerSocket ss = null; try { ss = new ServerSocket(); ss.bind(null); return ss.getLocalPort(); } catch (IOException e) { throw new IllegalStateException("", e); } finally { ... |
int | getAvailablePort(final int basePort) get Available Port if (basePort == 0) { return getAvailablePort(); if (basePort < 0 || basePort > 65535) { throw new IllegalArgumentException("Illegal port number: " + basePort); for (int i = basePort; i <= 65535; i++) { if (isPortAvailable(i)) { ... |
int | getAvailablePort(int defaultPort) get Available Port try { ServerSocket ss = new ServerSocket(defaultPort); ss.close(); return defaultPort; } catch (IOException e) { ServerSocket ss = new ServerSocket(0); int port = ss.getLocalPort(); ... |
int | getAvailablePort(int port) get Available Port int result = port; while (true) { if (isPortAvailable(result)) { break; result++; return result; ... |
Integer | getAvailablePort(int port) get Available 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 { ... |
int | getAvailablePort(int prefferedPort) get Available Port int localPort = -1; try (ServerSocket socket = new ServerSocket(prefferedPort)) { localPort = socket.getLocalPort(); } catch (IOException exp) { if (prefferedPort > 0) { return getAvailablePort(0); return localPort; |
int | getAvailablePort(int randomAttempts) Attempts to find a free port between the MIN_PORT_NUMBER(9000) and MAX_PORT_NUMBER(11000). ArrayList<Integer> failedPorts = new ArrayList<Integer>(randomAttempts); Random randomNum = new Random(); int randomPort = MAX_PORT_NUMBER; while (randomAttempts > 0) { randomPort = randomNum.nextInt(MAX_PORT_NUMBER - MIN_PORT_NUMBER) + MIN_PORT_NUMBER; if (checkIfPortAvailable(randomPort)) { return randomPort; failedPorts.add(randomPort); randomAttempts--; randomPort = MAX_PORT_NUMBER; while (true) { if (!failedPorts.contains(randomPort) && checkIfPortAvailable(randomPort)) { return randomPort; randomPort--; |
int | getAvailablePort(int startPort) Find the first available port after a given port int port = startPort; while (_assignedPorts.contains(port) && !portAvailable(port) && port < 0x7FFFFFFF) { port++; if (0x7FFFFFFF == port) { throw new RuntimeException("no available port found starting at " + startPort); _assignedPorts.add(port); ... |
int | getAvailablePort(String hostname, String portRange) get Available Port StringTokenizer st = new StringTokenizer(portRange, ","); while (st.hasMoreTokens()) { int index = portRange.indexOf('-'); if (index == -1) { int port = Integer.parseInt(st.nextToken().trim()); if (port == 0) { return 0; try { return getPort(port); } catch (IOException e) { } else { int startPort = Integer.parseInt(portRange.substring(0, index)); int endPort = Integer.parseInt(portRange.substring(index + 1)); if (endPort < startPort) { throw new IllegalArgumentException( "Start port [" + startPort + "] must be greater than end port [" + endPort + "]"); for (int i = startPort; i <= endPort; i++) { try { return getPort(i); } catch (IOException ex) { throw new IOException("Failed to get free port from [" + portRange + "]"); |
List | getAvailablePorts(int basePort, int portCount) get Available Ports List<Integer> availablePorts = new ArrayList<Integer>(); int port = basePort; for (int i = 0; i < portCount; i++) { while (!isPortAvailable(port)) { port++; availablePorts.add(port++); return availablePorts; |