Skip to content

Instantly share code, notes, and snippets.

@ingramchen
Created February 2, 2015 03:57
Show Gist options
  • Save ingramchen/adacc38c22f6506c0053 to your computer and use it in GitHub Desktop.
Save ingramchen/adacc38c22f6506c0053 to your computer and use it in GitHub Desktop.
Java scan available port
// good for unit test
public static int scanAvailablePort() throws IOException {
try (ServerSocket serverSocket = new ServerSocket(0)) {
return serverSocket.getLocalPort();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment