udp6 - I am doing it wrong... pasted by C-Keen on Sat Apr 6 17:16:52 2013

;; The Problem
;;
;; I thought I could start a listener on a udp port in the background of a running csi.
;; So I wrote the script below (supposedly saved in a file "t.scm") and loaded that with csi -n t.scm
;; However it seems that the primordeal thread is not scheduled again unless I send some data
;; to the port (for example with "echo -n hi | nc -u localhost 1234"
;;
;; The udp6 documentation implies that udp-recvfrom will only block the current thread
;; So now I wonder: Am I using srfi-18 wrongly here?
;; Uncommenting the thread-wait-for-i/o! and/or the thread-yield! lines will make the repl respond after each packet, handling user input once then returns control to the thread

(use (srfi 18) socket udp6)

(define s
  (let ((s (udp-open-socket)))
    (udp-bind! s #f 1234)
    s))

(define (t1)
;  (thread-wait-for-i/o! (socket-fileno s))
  (receive (n data host port) (udp-recvfrom s 128)
           (print "Got " n " bytes of data from " host)
;           (thread-yield!)
           (t1)))

(thread-start! (make-thread (lambda () (t1))))
(print "thread started")

no title added by rivo on Sat Apr 6 17:41:56 2013

(define (t1)
 (when (socket-receive-ready? s)
  (receive (n data host port) (udp-recvfrom s 128)
           (print "Got " n " bytes of data from " host)))
 (thread-yield!)
 (t1))