Skip to content

Instantly share code, notes, and snippets.

@imjacobclark
Last active February 10, 2019 12:59
Show Gist options
  • Save imjacobclark/cc68d5803ee96b80576a4c8b0e4a55ab to your computer and use it in GitHub Desktop.
Save imjacobclark/cc68d5803ee96b80576a4c8b0e4a55ab 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* 8)
; 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*)
; 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
(sb-bsd-sockets:socket-receive accepted-socket *receive-buffer* (length *receive-buffer*))
(sb-bsd-sockets:socket-receive accepted-socket *receive-buffer* (length *receive-buffer*))
; Convert our receive from octets to string and
(print (octets-to-string *receive-buffer*))
; 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