aboutsummaryrefslogtreecommitdiff
path: root/server/tcp.lisp
blob: f8aeb4d59a19beaff9d295c2a2df38d059b0440d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)))))