I'm trying to figure out how to work with sockets in lisp.
I found the following code, which works fine.
https://www.nicklevine.org/lisp-book/co ... chhttp.pdf
But I ran into some problems as I tried sending a file instead of just strings.
I can send the file, but I haven't managed to report back to the client that the file was received.
Those are the functions I have changed:
Code: Select all
(defun handle-request (stream )
(with-open-file (file "newpicture.jpg" :direction :output :element-type :default :if-exists :supersede)
(loop for c = (read-byte stream nil)
while c do (write-byte c file)))
(force-output stream))
(defun simple-test (port filepathname)
(let* ((socket (usocket:socket-connect #(127 0 0 1) port :element-type :default))
(stream (usocket:socket-stream socket)))
(with-open-file (file filepathname :element-type 'unsigned-byte)
(loop for c = (read-byte file nil)
while c do (write-byte c stream)))
(force-output stream)
(close stream)
(usocket:socket-close socket)))
Code: Select all
(defun handle-file-request (stream )
(with-open-file (file "newpicture.jpg" :direction :output :element-type :default :if-exists :supersede)
(loop for c = (read-byte stream nil)
while c do (write-byte c file)))
(print "file received" stream) ;; new
(force-output stream))
(defun simple-file-test (port filepathname)
(let* ((socket (usocket:socket-connect #(127 0 0 1) port :element-type :default))
(stream (usocket:socket-stream socket)))
(with-open-file (file filepathname :element-type 'unsigned-byte)
(loop for c = (read-byte file nil)
while c do (write-byte c stream)))
(force-output stream)
(let ((result (read-line stream))) ;; new
(close stream)
(usocket:socket-close socket))
result))
I'm using SBCL.
Thank you!