mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-19 21:59:17 +02:00
They can be close()d explicitly with close-file-descriptor, and will close() on gc, but only explicitly closing the last port on them will close the fileno. Notably needed for network sockets where we open separate input and output ports on the same socket.
26 lines
862 B
Scheme
Executable file
26 lines
862 B
Scheme
Executable file
#!/usr/bin/env chibi-scheme
|
|
|
|
;; Simple R7RS echo server, using the run-net-server utility from
|
|
;; (chibi net server).
|
|
|
|
(import (scheme base) (scheme write) (chibi net) (chibi net server))
|
|
|
|
;; Copy each input line to output.
|
|
(define (echo-handler in out sock addr)
|
|
(let ((line (read-line in)))
|
|
(cond
|
|
((not (or (eof-object? line) (equal? line "")))
|
|
;; log the request to stdout
|
|
(display "read: ") (write line)
|
|
(display " from ")
|
|
(display (sockaddr-name (address-info-address addr)))
|
|
(display ":") (write (sockaddr-port (address-info-address addr)))
|
|
(newline)
|
|
;; write and flush the response
|
|
(display line out)
|
|
(newline out)
|
|
(flush-output-port out)
|
|
(echo-handler in out sock addr)))))
|
|
|
|
;; Start the server on *:5556 dispatching clients to echo-handler.
|
|
(run-net-server 5556 echo-handler)
|