#! /bin/sh
exec sizzle -s $0 $*
!#
(use-modules (core net))

;; The following is taken from the Guile mailing list without
;; modification, except for an additional call to `force-output' after
;; each line sent.

;;;
;;; From: Gary Houston <ghouston@actrix.gen.nz>
;;; Subject: Re: Socket programming
;;; To: szi@aibon.ping.de
;;; Cc: guile@cygnus.com
;;; Date: 30 Sep 1997 23:17:27 -0000
;;;
;;; | Date: Tue, 30 Sep 1997 21:34:27 +0100
;;; | From: Sascha Ziemann <szi@aibon.ping.de>
;;; |
;;; | Hi,
;;; |
;;; | the Perl book contains a example program that shows how to write a
;;; | simple daemon. Does such an example exist for Guile too?
;;; |
;;;
;;; Here's a complicated way to write a simple daemon.  talk to it by
;;; telnetting to port 20004:

(let ((port (socket AF_INET SOCK_STREAM 0))
      (inet-port 20004))          ; Some unused port.
  (bind port AF_INET INADDR_ANY inet-port)
  (listen port 10)                ; Queue up to 10 requests.
  (let loop ()
    (let ((p (accept port)))
      (display "Incoming connection from ")
      (display (inet-ntoa (sockaddr:addr (cdr p))))
      (display " port ")
      (display (sockaddr:port (cdr p)))
      (newline)
      (let next-line ((line (read-line (car p))))
	(cond ((eof-object? line)
	       (display "EOF\n"))
	      (else
	       (display line)
	       (newline)
	       (display "you sent:\n" (car p))
	       (display line (car p))
	       (newline (car p))
	       (force-output (car p))	; needed for Sizzle sockets (mgrabmue)
	       (next-line (read-line (car p))))))
      (close-port (car p)))
      (loop)))

