Last active
February 21, 2023 16:16
-
-
Save saniaky/f34cf02f4d007c2253ce99e890cb5c9d to your computer and use it in GitHub Desktop.
Check if port is open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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