public static int getNextAvailablePort(int fromPort, int toPort) {
        if (fromPort < MIN_PORT_NUMBER || toPort > MAX_PORT_NUMBER || fromPort > toPort) {
            throw new IllegalArgumentException(format("Invalid port range: %d ~ %d", fromPort, toPort));
        }

        int nextPort = fromPort;
        //noinspection StatementWithEmptyBody
        while (!isPortAvailable(nextPort++)) { /* Empty */ }
        return nextPort;
    }
public static int getNextAvailablePort ( int fromPort , int toPort ) { if ( fromPort < MIN_PORT_NUMBER || toPort > MAX_PORT_NUMBER || fromPort > toPort ) { throw new IllegalArgumentException ( format ( "Invalid port range: %d ~ %d" , fromPort , toPort ) ) ; } int nextPort = fromPort ; while ( ! isPortAvailable ( nextPort ++ ) ) { } return nextPort ; }
Returns a currently available port number in the specified range.
@param fromPort the lower port limit
@param toPort the upper port limit
@throws IllegalArgumentException if port range is not between {@link #MIN_PORT_NUMBER}
and {@link #MAX_PORT_NUMBER} or <code>fromPort</code> if greater than <code>toPort</code>.
@return an available port