From 409897562cfb59a7181226fb3b60f216f9032e9e Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Thu, 9 May 2019 21:10:11 +0200 Subject: [PATCH] Add tcp server example --- server/tcp.lisp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 server/tcp.lisp diff --git a/server/tcp.lisp b/server/tcp.lisp new file mode 100644 index 0000000..f8aeb4d --- /dev/null +++ b/server/tcp.lisp @@ -0,0 +1,38 @@ +(ql:quickload "usocket") +(ql:quickload "babel") + + +(defparameter buf (make-array 10 + :element-type '(unsigned-byte 8) + :adjustable nil + :fill-pointer nil + :displaced-to nil + :initial-element 0)) + ;(defparameter s (usocket:socket-listen "localhost" 8080)) + +;(defparameter su (usocket:socket-connect nil nil :protocol :stream :local-host "localhost" :local-port 8080)) + +;; (defun display-buf (buf) +;; (let* ((message (flexi-streams:octets-to-string buf :external-format :utf-8 :end 10)) +;; (trimmed-message (trim message))) +;; (format t "~a" trimmed-message))) + + + + +(defun create-server (port) + (let* ((socket (usocket:socket-listen "127.0.0.1" port)) + (connection (usocket:socket-accept socket :element-type 'character))) + (unwind-protect + (progn + (let ((data (read-line (usocket:socket-stream connection)))) + (loop while data + do + (format t "~A~%" data) + (setf data (read-line (usocket:socket-stream connection) nil nil))))) +; (format (usocket:socket-stream connection) "Hello World~%") + ; (force-output (usocket:socket-stream connection)))) + (progn + (format t "Closing sockets~%") + (usocket:socket-close connection) + (usocket:socket-close socket)))))