Java Utililty Methods HTTP Port Find

List of utility methods to do HTTP Port Find

Description

The list of methods to do HTTP Port Find are organized into topic(s).

Method

intavailablePort(int prefered)
Check whether the port is available to binding
int rtn = -1;
try {
    rtn = tryPort(prefered);
} catch (IOException e) {
return rtn;
booleancanConnect(String host, int port)
can Connect
InetSocketAddress localAddress = new InetSocketAddress(0);
InetSocketAddress remoteAddress = new InetSocketAddress(host, Integer.valueOf(port));
return _canConnect(localAddress, remoteAddress);
booleancanConnect(String host, int port)
can Connect
InetSocketAddress address = new InetSocketAddress(host, Integer.valueOf(port));
InetSocketAddress local = new InetSocketAddress(0);
InputStream in = null;
try (Socket socket = new Socket()) {
    socket.bind(local);
    socket.connect(address, 3000);
    in = socket.getInputStream();
    return true;
...
booleancanConnectOn(String host, int port)
can Connect On
try {
    Socket socket = new Socket(host, port);
    socket.close();
    return true;
} catch (Exception ex) {
    return false;
InetSocketAddresscreateUnresolved(String host, int port)
Creates and returns an unresolved InetSocketAddress .
return InetSocketAddress.createUnresolved(host, port);
voiddoSend(String command, String server, String port)
do Send
Socket socket = new Socket(server, Integer.parseInt(port));
OutputStream os = socket.getOutputStream();
BufferedOutputStream out = new BufferedOutputStream(os);
out.write(command.getBytes());
out.write('\r');
out.flush();
socket.close();
StringexecuteHttpCommand(String host, int port, String request, String charset)
Connect to given host:port via direct TCP socket, send the input string, and return the result (if any) as a string
Objects.requireNonNull(host);
Objects.requireNonNull(request);
try (Socket socket = new Socket(host, port)) {
    socket.getOutputStream().write(request.getBytes(charset));
    socket.shutdownOutput();
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), charset));
    String response = "";
    String line = in.readLine();
...
voidexportResource(Class fromClass, String resourceName, String exportPath)
export Resource
InputStream in = null;
OutputStream out = null;
try {
    in = fromClass.getResourceAsStream(resourceName);
    if (in == null) {
        throw new IOException("Cannot get resource \"" + resourceName + "\" from jar.");
    int readBytes;
...
intfindAvailablePort()
find Available Port
return findAvailablePort(21111);
intfindAvailablePort()
Requests a random server socket port to be created, closes it, and returns the port picked as a potentially available port.
int port = 0;
ServerSocket socket = null;
try {
    socket = new ServerSocket(0);
    socket.setReuseAddress(true);
    port = socket.getLocalPort();
    Logger.getAnonymousLogger().info("port candidate:" + port);
} catch (Throwable e) {
...