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

[shootout] echo.scm + Question concerning the implementation of thread

Here is a go at the echo server.
I have used threads although the guide lines tell you to avoid them;
fork was no where in the documentation (and frankly I like threads better).
The main reason for avoiding threads is the the timing is easier if
the programs only run in one thread on the host OS. The documentation
of  threads in mzscheme states that the implementation depends on the
Does that mean that mzscheme use a single thread on Windows and mulitple

/Jens Axel Søgaard

Guide lines

    Each test program runs a single-threaded server process that listens on
an Internet
    socket. When the server starts, it forks a separate client process that
connects back
    to the server's socket. Then the server loops, receiving data and
echoing it back to
    the client until the client closes its socket. When it is finished, the
server reports how
    many bytes were processed. The client checks each echo request sent
against the data
     received from the server to make sure it is correct before the next
request is made.
    Each echo request consists of 19 characters including a terminating
    Each client must do the following for N iterations:

   send 19 bytes
   recv 19 bytes
   check reply == request

    The read/write operations on the stream socket should either use
line-oriented standard I/O functions or else handle incomplete reads/writes.
    The reason for using fork is simply to make it easy for the test driver,
so it only has to start one process.

; @echo off
; mzscheme.exe -f echo.bat -mvC %1
; goto :end

(define PORT 8888)
(define DATA "Hello there sailor\n")
(define n 10)

(define (server)
  (thread client)
  (let-values ([(in out) (tcp-accept (tcp-listen PORT))])
    (let loop ([i (read in)]
               [bytes 0])
      (if (not (eof-object? i))
            (write i out)
            (loop (read in)
                  (+ bytes (string-length i))))
            (display "server processed ")
            (display bytes)
            (display " bytes\n"))))))

(define (client)
  (let-values ([(in out) (tcp-connect "" PORT)])
    (let loop ([n n])
      (if (> n 0)
            (write DATA out)
            (let ([i (read in)])
                (if (equal? DATA i)
                    (loop (- n 1))
          (close-output-port out)))))

(define (main args)
  (set! n (string->number (car  args)))

; :end