NetUtil { public static boolean checkPort(String host, int port) { if (!DomainValidator.getInstance().isValid(host) && !InetAddressValidator.getInstance().isValid(host)) throw new IllegalArgumentException("Invalid host: " + host); if (!ValidationUtil.port(port)) throw new IllegalArgumentException("Invalid port: " + port); try (Socket sock = new Socket()) { sock.connect(new InetSocketAddress(host, port), PORTCHECK_TIMEOUT); return sock.isConnected(); } catch (UnknownHostException e) { return false; } catch (IOException e) { return false; } } private  NetUtil(); static boolean checkPort(String host, int port); static byte[] download(String url); static void download(String url, File file); static byte[] download(URL url); static void download(URL url, File file); static Optional<String> serviceName(int port);  }
@Test @DisplayName("Check some localhost ports") void checkPort_1() throws IOException { assertFalse(checkPort("127.0.0.1", 8923)); try (ServerSocket socket = new ServerSocket(8923)) { assertTrue(checkPort("127.0.0.1", 8923)); } assertFalse(checkPort("127.0.0.1", 8923)); }