remote-snake/server/tcp.lisp
2019-05-09 21:10:11 +02:00

38 lines
1.4 KiB
Common Lisp

(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)))))