Created
March 16, 2012 14:16
-
-
Save fukamachi/2050237 to your computer and use it in GitHub Desktop.
Find port number not in use
This file contains hidden or 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
(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