Lisp Simple Socket Client
with-open-socket を使用して Socket のクライアントを書く例。昨日のHTTP Request Reader - with-open-socket version - Shammerismに一発リクエストを送ってみる。
(defun send-message (destination-host destination-port message) (with-open-socket (client :address-family :internet :type :stream :connect :active :remote-host destination-host :remote-port destination-port) (format client message) (format client "~%") (force-output client))) (send-message "localhost" 8080 "Test")
当然だが、事前に昨日のサーバは起動しておかないといけない。サーバー側はこんな感じ。
$ ccl64 --load echo-server.lisp > Start OpenEchoServer... > OpenEchoServer port open... > Server received a message: Test > Received Complete... Welcome to Clozure Common Lisp...
クライアントはこんな感じ。
$ ccl64 --load echo-client.lisp Welcome to Clozure Common Lisp ... ?
クライアントは応答を受け取っていないから、起動時に一回リクエストを送信して終了する感じだ。応答を受け取るように改良したらいろいろ楽しいかもしれないが、サーバーが HTTP Request を想定しているからそれに合わせてリクエストを送信しないといけない。