[Top][All Lists]

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

Re: [MIT-Scheme-users] Simple Client/Server

From: Amit Saha
Subject: Re: [MIT-Scheme-users] Simple Client/Server
Date: Tue, 28 Apr 2009 22:47:41 +0530

Hello Mark,

On Tue, Apr 28, 2009 at 8:34 AM, Matt Birkholz
<address@hidden> wrote:
>> From: Taylor R Campbell <address@hidden>
>> Date: Mon, 27 Apr 2009 19:41:34 -0400
>> [...]
>> Nevertheless, as you suggest, it is good practice to close the
>> socket explicitly.
> So something like this would be more in order:
> (define (start-server service)
>  (let ((server-sock (open-tcp-server-socket service)))
>    (display "Server ready to accept connections.")(newline)
>    (let* ((port (tcp-server-connection-accept server-sock #t #f))
>           (from-client (read-line port)))
>      (display from-client port)(newline port)
>      (display from-client)(newline)
>      (close-port port))
>    (close-tcp-server-socket server-sock)))
> The above procedure echoes the line to the client AND the console.

Thanks. This works:



(define (start-server service)
(let ((server-sock (open-tcp-server-socket service)))
   (display "Server ready to accept connections..")
   (wait-for-connection server-sock)
   (close-tcp-server-socket server-sock)

(define (wait-for-connection server-sock)
(let* ((port (tcp-server-connection-accept server-sock #t #f))
     (from-client (read-line port)))
     (display from-client port) (newline port)
     (display from-client) (newline)
     ; (close-port port)
(wait-for-connection server-sock)


The tail call is to keep the server alive to accept more than one
connection. I intend to make this a multi-threaded server. As of now,
it echoes only the first message for each new connection. The
subsequent messages are not echoed back or displayed. I am guessing,
this is because the 'port' is re-bound everytime a new connection is

'telnet' works perfectly fine with the above server.

However, the following 'client.scm' file connects to the server, but
no data is transferred back and forth:



(define (start-client service)
  (let* ((remotesocket (open-tcp-stream-socket "localhost" service)))
    (display "From " remotesocket) (newline remotesocket)
  (close-port remotesocket))


What is going wrong?

Thank You once again.

*Bangalore Open Java Users Group*

"Recursion is the basic iteration mechanism in Scheme"

reply via email to

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