RSS Feed

Lisp Project of the Day

find-port

You can support this project by donating at:

Donate using PatreonDonate using Liberapay

Or see the list of project sponsors.

find-portnetwork

Documentation๐Ÿฅบ
Docstrings๐Ÿฅบ
Tests ๐Ÿคจ
Examples๐Ÿ˜€
RepositoryActivity๐Ÿ˜€
CI ๐Ÿ˜€

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!


Brought to you by 40Ants under Creative Commons License