Created
February 1, 2012 04:48
-
-
Save michaelklishin/1715167 to your computer and use it in GitHub Desktop.
Pick a first available port to bind to with Clojure
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
(defn- first-available-port | |
[] | |
;; java.net.ServerSocket bound to port 0 will pick the first available port automatically | |
;; http://docs.oracle.com/javase/6/docs/api/java/net/ServerSocket.html#ServerSocket(int). | |
(let [ss (java.net.ServerSocket. 0) | |
port (.getLocalPort ss)] | |
(.close ss) | |
port)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment