Last active
August 29, 2015 14:11
-
-
Save corehello/df62ebe00fb5c81738a0 to your computer and use it in GitHub Desktop.
run-eval-loop common-lisp socket server code from http://www.clisp.org/impnotes/socket.html#socket-ex-server
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
(LET ((server (SOCKET:SOCKET-SERVER))) | |
(FORMAT t "~&Waiting for a connection on ~S:~D~%" | |
(SOCKET:SOCKET-SERVER-HOST server) (SOCKET:SOCKET-SERVER-PORT server)) | |
(UNWIND-PROTECT | |
;; infinite loop, terminate with Control+C | |
(LOOP (WITH-OPEN-STREAM (socket (SOCKET:SOCKET-ACCEPT server)) | |
(MULTIPLE-VALUE-BIND (local-host local-port) (SOCKET:SOCKET-STREAM-LOCAL socket) | |
(MULTIPLE-VALUE-BIND (remote-host remote-port) (SOCKET:SOCKET-STREAM-PEER socket) | |
(FORMAT T "~&Connection: ~S:~D -- ~S:~D~%" | |
remote-host remote-port local-host local-port))) | |
;; loop is terminated when the remote host closes the connection or on EXT:EXIT | |
(LOOP (WHEN (EQ :eof (SOCKET:SOCKET-STATUS (cons socket :input))) (RETURN)) | |
(PRINT (EVAL (READ socket)) socket) | |
;; flush everything left in socket | |
(LOOP :for c = (READ-CHAR-NO-HANG socket nil nil) :while c) | |
(TERPRI socket)))) | |
;; make sure server is closed | |
(SOCKET:SOCKET-SERVER-CLOSE server))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment