guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How Many Different Ways Do I Suck?


From: Evan Prodromou
Subject: How Many Different Ways Do I Suck?
Date: 02 Jun 2001 14:22:17 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.0.103

So, I have a question here that's bugging me quite a bit. Attached
below is a simple script for running a threaded socket server. Sadly,
it does not work -- the dbg output usually stops in the middle of
printing out "Calling handler.". If I connect again from another
client, things chug along for a bit, and then stop again.

I'm wondering if the call to "accept" is blocking -all- threads in the
process, because of Guile's cooperative threading. Am I close? What
can I do? Is there a better way to run a server that can handle
multiple connections?

Any help would be appreciated.

~ESP

---8<---
#!/usr/bin/guile \
-e main -s
!#

(use-modules (ice-9 threads)
             (ice-9 format))

(define *debug-kwds* '(#:server #:thread #:echo))
(define *debug-mutex* (make-mutex))

(define (dbg kwd . args)
  (with-mutex
   *debug-mutex*
   (if (member kwd *debug-kwds*)
       (let ((p (current-error-port)))
         (apply format (cons p args))
         (newline p)
         (force-output p)
         #f))))

(define (run-socket-server handler port queue-len)
  (let ((sock (socket AF_INET SOCK_STREAM 0)))
    (bind sock AF_INET INADDR_ANY port)
    (listen sock queue-len)
    (let next-conn ((pr (accept sock)))
      (dbg #:server "Got connection.")
      (call-with-new-thread
       (lambda ()
         (dbg #:thread "Started thread.")
         (dbg #:thread "Calling handler.")
         (handler (car pr) (cdr pr))
         (dbg #:thread "Finished handler."))
       (lambda (errnum) (dbg #:thread "Got error.")))
      (dbg #:server "Listening for new connection.")
      (next-conn (accept sock)))))

(define (echo-handler sock client-info)
  (dbg #:echo "started handler on ~A with ~A." sock client-info)
  (let ((line (read-line sock)))
    (dbg #:echo "Got line: ~A." line)
    (write-line line sock)
    (force-output sock)
    (dbg #:echo "Wrote out line.")
    (close sock)))

(define (main args)
  (run-socket-server echo-handler 9005 5))
---8<---

-- 
Evan Prodromou
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]