List of utility methods to do HTTP Port Find
int | availablePort(int prefered) Check whether the port is available to binding int rtn = -1; try { rtn = tryPort(prefered); } catch (IOException e) { return rtn; |
boolean | canConnect(String host, int port) can Connect InetSocketAddress localAddress = new InetSocketAddress(0); InetSocketAddress remoteAddress = new InetSocketAddress(host, Integer.valueOf(port)); return _canConnect(localAddress, remoteAddress); |
boolean | canConnect(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; ... |
boolean | canConnectOn(String host, int port) can Connect On try { Socket socket = new Socket(host, port); socket.close(); return true; } catch (Exception ex) { return false; |
InetSocketAddress | createUnresolved(String host, int port) Creates and returns an unresolved InetSocketAddress . return InetSocketAddress.createUnresolved(host, port);
|
void | doSend(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(); |
String | executeHttpCommand(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(); ... |
void | exportResource(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; ... |
int | findAvailablePort() find Available Port return findAvailablePort(21111);
|
int | findAvailablePort() 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) { ... |