CLISP echo-server & echo-client
CLISP による echo-server と echo-client。
サーバーの実装は32.4. Socket Streamsをベースに、というかほとんどコピー。read-eval-print の部分だけを少し書き換え。
(let ((server (socket:socket-server $listen-port))) (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 (client (socket:socket-accept server)) (multiple-value-bind (local-host local-port) (socket:socket-stream-local client) (multiple-value-bind (remote-host remote-port) (socket:socket-stream-peer client) (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 client :input))) (return)) (let ((line (read-line client nil 'eof))) (format t "~A~%" line))))) ;; make sure server is closed (socket:socket-server-close server)))
クライアントは以下のような感じ。
(with-open-stream (client (socket:socket-connect $destination-port $destination-host :external-format :DOS)) (princ message client)))