Code: Select all
(defun tcp-server ()
(with-socket-listener (socket "127.0.0.1" 8888 :element-type '(unsigned-byte 8))
(loop
(with-connected-socket (connection (socket-accept socket))
(format t "Server received connection" )))))
Code: Select all
(defmacro with-connected-socket ((var socket) &body body)
"Bind `socket' to `var', ensuring socket destruction on exit.
`body' is only evaluated when `var' is bound to a non-null value.
The `body' is an implied progn form."
`(let ((,var ,socket))
(unwind-protect
(when ,var
(with-mapped-conditions (,var)
,@body))
(when ,var
(socket-close ,var)))))
Is that right? The macros make it all a little magical to me
