[gambit-list] thread input/output to port

Marc Feeley feeley at iro.umontreal.ca
Wed Oct 14 09:40:29 EDT 2009


On 2009-10-13, at 8:00 PM, lowly coder wrote:

> Sorry for dumb question --
>
> I have a tcp port. I'm starting a thread. How can I redirect all
> input/output from/to the thread to this tcp port?
>
> somethign like
>
>
> (let ((p (pre existing code that makes bidirectional port))
>  .. modify the following ...
>  (thread-start! (make-thread func)))

Here are three ways to do this.

Marc

(define (server1)
   (tcp-service-register!
    11111
    (lambda () (display "Hello\n"))))

(define (server2)
   (let ((s (open-tcp-server 22222)))
     (let loop ()
       (let ((connection (read s)))
         (thread-start!
          (make-root-thread
           (lambda ()
             (display "World\n") ;; will go to "connection"
             (close-input-port (current-input-port))
             (close-output-port (current-output-port)))
           'hello
           (thread-thread-group (current-thread))
           connection
           connection)))
       (loop))))

(define (server3)
   (let ((s (open-tcp-server 33333)))
     (let loop ()
       (let ((connection (read s)))
         (parameterize ((current-input-port connection)
                        (current-output-port connection))
           (thread-start!
            (make-thread
             (lambda ()
               (display "Bonjour\n") ;; will go to "connection"
               (close-input-port (current-input-port))
               (close-output-port (current-output-port)))
             'hello))))
       (loop))))

(server1)
(thread-start! (make-thread server2))
(server3)



More information about the Gambit-list mailing list