Java HTTP Port Find available_port()

Here you can find the source of available_port()

Description

availablport

License

Apache License

Declaration

public static int available_port() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.net.ServerSocket;

public class Main {
    public static int available_port() {
        return available_port(0);
    }//w  ww.j a  va 2 s  .c o  m

    public static int available_port(int prefered) {
        int rtn = -1;
        try {
            rtn = try_port(prefered);
        } catch (IOException e) {
            rtn = available_port();
        }
        return rtn;
    }

    public static int try_port(int port) throws IOException {
        ServerSocket socket = new ServerSocket(port);
        int rtn = socket.getLocalPort();
        socket.close();
        return rtn;
    }
}

Related

  1. available(final int port)
  2. available(int port)
  3. available(int port)
  4. available(int port)
  5. available(int port)
  6. availableAndReturn(int MIN_PORT_NUMBER, int MAX_PORT_NUMBER)
  7. availablePort()
  8. availablePort(int port)
  9. availablePort(int pPort)