Skip to content

Instantly share code, notes, and snippets.

@hugoduncan
Created April 23, 2012 03:10
Show Gist options
  • Save hugoduncan/2468613 to your computer and use it in GitHub Desktop.
Save hugoduncan/2468613 to your computer and use it in GitHub Desktop.
Test for a reachable port
(defn port-reachable?
([ip port timeout]
(let [socket (doto (java.net.Socket.)
(.setReuseAddress false)
(.setSoLinger false 1)
(.setSoTimeout timeout))]
(try
(.connect socket (java.net.InetSocketAddress. ip port))
true
(catch java.io.IOException _)
(finally
(try (.close socket) (catch java.io.IOException _))))))
([ip port]
(port-reachable? ip port 2000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment