Skip to content

Instantly share code, notes, and snippets.

@michaelklishin
Created February 1, 2012 04:48
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 michaelklishin/1715167 to your computer and use it in GitHub Desktop.
Save michaelklishin/1715167 to your computer and use it in GitHub Desktop.
Pick a first available port to bind to with Clojure
(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