Skip to content

Latest commit

 

History

History
54 lines (41 loc) · 1.27 KB

0048-find-port.org

File metadata and controls

54 lines (41 loc) · 1.27 KB

find-port

This small utility library allows you to find an open network port or to probe if the port is open.

It is useful when you need to start a server on some free port in a given range:

POFTHEDAY> (find-port:find-port :min 4005)
4005 (12 bits, #xFA5)

;; Now we'll start the netcat process
;; to listen on the 4005 port
POFTHEDAY> (bt:make-thread
            (lambda ()
              (uiop:run-program
               "nc -l 127.0.0.1 4005")))
#<SB-THREAD:THREAD "Anonymous thread" RUNNING {10050A25F3}>

;; Port 4005 is taken now:
POFTHEDAY> (find-port:find-port :min 4005)
4006 (12 bits, #xFA6)

;; This function can be used to check if the port is taken:
POFTHEDAY> (find-port:port-open-p 4005)
NIL
POFTHEDAY> (find-port:port-open-p 4006)
T
  

Beware, this library considered port is taken only when this port is opened by another process. This is because it uses :reuse-address argument when checking if the port is opened:

(usocket:socket-listen interface port
                       :reuse-address t)

That is it for this Friday. I have to spend some time with my family. Have a nice weekend!