[plt-scheme] A problem about TCP program

From: Plato Wu (gtalk000 at gmail.com)
Date: Sun Jan 10 02:21:50 EST 2010

I want to develop a simple TCP program in PLT Scheme. First I try
the example from a tutorial: 
http://www.free-soft.org/FSM/english/issue01/sk.html 

The code is:
#lang scheme
(require scheme/tcp)

(define SERVICE-PORT 2005)
(define SERVER-HOST "localhost")

(define token-client
    (lambda (how-many-tokens)
      (let-values ([(server->me me->server)
                  (tcp-connect SERVER-HOST SERVICE-PORT)])
        (let loop ([how-many-more how-many-tokens])
          (if (<= how-many-more 0)
            (begin
              (close-input-port server->me)
              (write 'enough me->server)
              (close-output-port me->server)
              '())
            (begin
              (write 'more me->server)
              (newline me->server)
              (flush-output me->server)
              (cons (read server->me)
                    (loop (- how-many-more 1)))))))))

 
 (define server
    (let ([next-token -1])
      (lambda ()
      (let ([listener (tcp-listen SERVICE-PORT)])
          (let server-loop ()
              (let-values ([(client->me me->client)
                                          (tcp-accept listener)])
              (let per-client-loop ()
                (let ([request (read client->me)])
                  (case request
                    [(enough)
                                 (close-input-port client->me)
                                              (close-output-port
    me->client)
                     (server-loop)]
                    [(more)
                     (set! next-token (+ next-token 1))
                     (write next-token me->client)
                     (newline me->client)
                     (per-client-loop)])))))))))

I run it in the Interaction:
> (thread server)
#<thread:server>
> (token-client 5)
. . user break

It will hang until I press stop button, it stops at (read client->me).

My PLT Scheme version is 4.1.3.

Thank you in advance!

Best wishes
Plato Wu


Posted on the users mailing list.