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

From: Cyprien Nicolas (c.nicolas at gmail.com)
Date: Fri Jul 20 13:34:52 EDT 2007

2007/7/20, Erich Rast

> 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?

You can use the exception system to catch errors, with
(call-with-exception-handler f thunk) or with-handlers (see

If an exception is raised when executing thunk, f will be called with
the exception as argument, and if that is an io-error, just try

Maybe there are another ways, but somehow, there are some case you
don't have choice, eg: how to know if a computer is connected to the
web even if the interfaces are loaded ? try to resolve a domain, and
catch the error.

Cyprien Nicolas

Posted on the users mailing list.