Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.