Skip to content

Instantly share code, notes, and snippets.

@fukamachi
Created March 16, 2012 14:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fukamachi/2050237 to your computer and use it in GitHub Desktop.
Save fukamachi/2050237 to your computer and use it in GitHub Desktop.
Find port number not in use
(defun port-available-p (port)
(handler-case (let ((socket (usocket:socket-listen "127.0.0.1" port :reuse-address t)))
(usocket:socket-close socket))
(usocket:address-in-use-error (e) (declare (ignore e)) nil)))
;; Find port number not in use from 50000 to 60000.
(loop for port from (+ 50000 (random 1000)) upto 60000
if (port-available-p port)
return port)
;=> 50177
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment