[Haskell-cafe] detecting socket closure in haskell

Donn Cave donn at avvanta.com
Thu Dec 4 20:15:01 EST 2008


Quoth "Tim Docker" <timd at macquarie.com.au>:

| Hence I seem to need a means of detecting that a socket has been closed
| remotely, without actually reading from it. . Does anyone know how to do
| this? One reference I've found is this:
|
| http://stefan.buettcher.org/cs/conn_closed.html
|
| Apparently recv() with appropriate flags can detect this - though I'm
| not convinced that the code as shown on that page doesn't busy wait when
| there is unread data from the client.

Right - poll() will still find data there, after the "peek", so you'll
be running that loop as fast as you can make the 2 system calls, and
what I think might be worse, it should keep reading the same 32 bytes
at the head of the buffer ... and never notice the connection close.
Well, I haven't tried it, but this solution doesn't make sense to me.

| Any tips or pointers? Perhaps what I'm trying to do is not currently
| possible in haskell, without using the FFI (which would be ok). Or
| perhaps it's not possible in linux at all.

I don't know of a way to do it in C.  I think it's very likely that the
kernel finds out only after all the data has been delivered through the
network interface, so by the time you can even in principle know that
the connection closed, all the data will either have been processed by
the client or reside in memory somewhere on the computer.

I guess you might try your buffering thread, and if you're really worried
about undue amounts of buffered data, set some maximum size, where you
either stop trying to notice end-of-file, or switch to another storage
medium (e.g., disk file.)

	Donn


More information about the Haskell-Cafe mailing list