Node:Internet Socket Server, Previous:Internet Socket Client, Up:Internet Socket Examples Internet Socket Server Example

The following example shows a simple Internet server which listens on port 2904 for incoming connections and sends a greeting back to the client.

(let ((s (socket AF_INET SOCK_STREAM 0)))
  (setsockopt s SOL_SOCKET SO_REUSEADDR 1)
  ;; Specific address?
  ;; (bind s AF_INET (inet-aton "") 2904)
  (bind s AF_INET INADDR_ANY 2904)
  (listen s 5)

  (simple-format #t "Listening for clients in pid: ~S" (getpid))

  (while #t
         (let* ((client-connection (accept s))
                (client-details (cdr client-connection))
                (client (car client-connection)))
           (simple-format #t "Got new client connection: ~S"
           (simple-format #t "Client address: ~S"
                           (sockaddr:addr client-details)))
           ;; Send back the greeting to the client port
           (display "Hello client\r\n" client)
           (close client))))