Created
June 6, 2016 08:49
-
-
Save kasimok/931edbce477654fc209227733b2efbf2 to your computer and use it in GitHub Desktop.
Method to test port opening and listening
This file contains 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 available(int port) { | |
if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) { | |
throw new IllegalArgumentException("Invalid start port: " + port); | |
} | |
ServerSocket ss = null; | |
DatagramSocket ds = null; | |
try { | |
ss = new ServerSocket(port); | |
ss.setReuseAddress(true); | |
ds = new DatagramSocket(port); | |
ds.setReuseAddress(true); | |
return true; | |
} catch (IOException e) { | |
} finally { | |
if (ds != null) { | |
ds.close(); | |
} | |
if (ss != null) { | |
try { | |
ss.close(); | |
} catch (IOException e) { | |
/* should not be thrown */ | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment