Skip to content

Instantly share code, notes, and snippets.

@saniaky
Last active February 21, 2023 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saniaky/f34cf02f4d007c2253ce99e890cb5c9d to your computer and use it in GitHub Desktop.
Save saniaky/f34cf02f4d007c2253ce99e890cb5c9d to your computer and use it in GitHub Desktop.
Check if port is open
public static boolean serverListening(String host, int port) {
Socket s = null;
try {
s = new Socket(host, port);
log.info("OK -> HOST: {}, PORT: {}", host, port);
return true;
} catch (Exception e) {
log.info("CLOSED -> HOST: {}, PORT: {}", host, port);
return false;
} finally {
if (s != null) {
try {
s.close();
} catch (Exception e) {
log.error("Error", e);
}
}
}
}
@Test
public void someTest() {
assumeTrue(serverListening("localhost", 22));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment