List of utility methods to do HTTP Port Find
int | safePort(final String port) Validate the given port is in a valid range, returning 0 if it is invalid. try { if ("*".equals(port)) { return randomPort(); final int number = Integer.parseInt(port); if (number < 0 || number > 65535) { return 0; } else { ... |
int | searchPort(int number, int even, boolean stream) Searches for a number of successive free ports in the range between minPort and maxPort boolean found = false; int offset = 0; int port = minPort; while (!found) { switch (even) { case 2: port = minPort + 2 * offset; break; ... |
int | selectAvailablePort(int preferedPort) select Available Port int port = -1; ServerSocket ss = null; try { ss = new ServerSocket(); ss.bind(new InetSocketAddress(preferedPort)); port = ss.getLocalPort(); } catch (IOException e) { try { ... |
String | sendCommand(final String command, final int monitorPort) send Command try (Socket socket = new Socket("127.0.0.1", monitorPort); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) { writer.write(command); writer.newLine(); writer.flush(); return reader.readLine(); |
void | sendCommand(String host, Integer port, String cmd) send Command Socket socket = new Socket(host, port);
OutputStream out = socket.getOutputStream();
out.write(cmd.getBytes());
out.close();
socket.close();
|
boolean | serverIsUp(int port) server Is Up try { URL url = new URL("http", "localhost", port, "/"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("GET"); httpURLConnection.connect(); OutputStream outputStream = httpURLConnection.getOutputStream(); ... |
boolean | ServerListening(String host, int port) The method check if a host is listening in the specified port. try (Socket s = new Socket(host, port);) { return true; } catch (Exception e) { return false; |
boolean | serverListening(String host, int port) server Listening Socket s = null; try { s = new Socket(host, port); return true; } catch (Exception e) { return false; } finally { if (s != null) { ... |
boolean | serverListening(String hostName, int port) Checks if there is a server socket listening in the given host and port. try { return serverListening(InetAddress.getByName(hostName), port); } catch (UnknownHostException e) { return false; |
void | waitForLiveServer(String webContainerHostname, int webContainerPort, int timeoutMin) wait For Live Server InetSocketAddress address = new InetSocketAddress(webContainerHostname, webContainerPort); System.out.println("Waiting " + timeoutMin + " min for web server..."); long beginTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis() + (timeoutMin * 60 * 1000); boolean portOpened = false; while ((!portOpened) && (System.currentTimeMillis() < endTime)) { portOpened = trySocket(address); if (portOpened) { ... |