[plt-scheme] How to close a connection gracefully when using a blocking read?
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
http://docs.plt-scheme.org/mzscheme/mzscheme-Z-H-6.html#node_chap_6)
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
port-closed?
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