Skip to content

Instantly share code, notes, and snippets.

@imjacobclark
Created February 10, 2019 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imjacobclark/89723165137efc5e506a4095fcaee7e9 to your computer and use it in GitHub Desktop.
Save imjacobclark/89723165137efc5e506a4095fcaee7e9 to your computer and use it in GitHub Desktop.
(require 'sb-bsd-sockets)
; Create an inet-socket instance
(defparameter *socket* (make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp))
; Define our address to be 0.0.0.0 (public interface)
(defparameter *address* '(0 0 0 0))
; Define our port to be 8080
(defparameter *port* 8080)
; Connections to hold on the backlog
(defparameter *socket-backlog* 100)
; Response to emit from the server
(defparameter *response* "hi")
; Length of the response
(defparameter *response-length* (length *response*))
; Receive buffer length
(defparameter *receive-buffer-lenth* 2)
; Buffer to recieve data sent from the client as an octet stream
(defparameter *receive-buffer* (make-array *receive-buffer-lenth* :element-type '(unsigned-byte 8) :initial-element 0))
; Bind our inet-socket to 0.0.0.0:8080
(sb-bsd-sockets:socket-bind *socket* *address* *port*)
; Start the socket listening on 0.0.0.0:8080 for connections
(sb-bsd-sockets:socket-listen *socket* *socket-backlog*)
(defun readdr(accepted-socket)
(nth-value 1 (sb-bsd-sockets:socket-receive accepted-socket *receive-buffer* (length *receive-buffer*)))
(print "h")
(print "t")
)
; Loop forever
(loop
(let (
; Accept incoming client connections
(accepted-socket (sb-bsd-sockets:socket-accept *socket*)))
; Recieve data as an octet stream into our receive buffer
(print (readdr accepted-socket))
(print (readdr accepted-socket))
; Respond to accepted client connections with a UTF-8 string of "hi" with length 2
(sb-bsd-sockets:socket-send accepted-socket *response* *response-length*)
; Close the client connection
(sb-bsd-sockets:socket-close accepted-socket)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment