Code Select
(load "libevent2.lsp")
(setf listen (net-listen 8000))
(unless listen
(throw-error (net-error)))
(libevent:init)
(libevent:watch listen libevent:READ
(lambda (fd e id , client)
; accept a new connection
(setf client (net-accept listen))
; watch for requests
(libevent:watch client libevent:READ
(lambda (fd e id , req)
; read request
(setf req (read-line fd))
; On a real server, here is where you would process the request,
; most likely in a secondary process. In that case, you would execute
; the code below when it finished, rather than immediately after
; the request comes in.
; wait for client to be ready to accept a response
(libevent:watch-once client libevent:WRITE
(lambda (fd e id)
; send response
(write fd req)))))))
(libevent:run)
A real server would have to watch for timeouts, disconnects, etc., as well, but this gives you the basic idea.