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

[plt-scheme] TCP/IP simple server in MZScheme - how to? (Plt 1.3p1 release)


I'm not very familiar with MzScheme yet and tried to create a simple 
server class to interface to a front end via TCP/IP. However, on Mac 
OS 9 in DrScheme and when I connect via telnet, the code below does 
display "connection" but only reads a few (5-6) list expressions and 
then the IDE quits with a low on memory message. Something in the 
methods start-listen-thread or data-available must be totally wrong.

Does anyone spot an obvious mistake in the code below? -- defclass* 
is just a simple wrapper macro to some (define name (class* ...)) and 
none of the other custom functions does anything unusual.

Is there some TCP/IP server sample for MZScheme?

Thanks in advance for any help and advices,


(define-struct client (in out))

(definterface wp:Interface<%> ()

(defclass* wp:Interface% wp:Object% (wp:Interface<%>)()
              [clients ()]

               (lambda ()
                  (let loop ()
                    (when (tcp-accept-ready? listener)
                        (set! clients
                              (cons (call-with-values
                                     (lambda ()
                                       (tcp-accept listener))
                                     (lambda (in out)
                                       (make-client in out)))
                        (display "connection")(newline))) ;debug
                    ; second loop part read data from clients
                     (lambda (client)
                       (data-available client (read (client-in client))))
                    (sleep 0.1)

               (lambda (client data)
                  (lambda ()
                    (cond ((list? data)
                           (display data) ;debug
                           (let ((cmd (make-object wp:Command%)))
                             (send cmd set-by-raw-data! data)
                             (received-command client cmd)))
                          (else (display "Unknown command: ")
                                (display data)
              (lambda ()
                (set! listener (tcp-listen (get-pref 'default-port)))
              (lambda (client cmd)
                (display (text 'debug-received-command
                               (send cmd debug-string))))]
              (lambda () (display (text 'debug-send-command)))]

(define a (make-object wp:Interface%))
(send a listen)