[plt-scheme] How to close a connection gracefully when using a blocking read?

From: Erich Rast (erich at snafu.de)
Date: Fri Jul 20 12:35:08 EDT 2007


My client listen loop looks like this:

  (define (connection-start-client-listen connection)
      (lambda ()
        (let loop ((in (read (connection-in-port connection))))
          (if (eof-object? in)
              (scp-info connection
                        "connection closed by remote host (end-of- 
                (scp-info connection "received ~s" (datum->string- 
truncated in (log-max-data-len)))
                (if (is-reply? in)
                    (handle-reply connection (car in))
                    (scp-warn connection "illegal reply: '~s'" (datum- 
 >string-truncated in (log-max-data-len))))
                (loop (read (connection-in-port connection)))))))))

The server uses a similar loop.

Here is the problem: When my client closes the connection, the server  
gracefully notices that in is an eof-object?. However, the client is  
also in a blocking wait, which results in an error "read-byte: input  
port is closed".

How can I close the connection without triggering an error on either  
the client or server side?



Posted on the users mailing list.