[Haskell-cafe] detecting socket closure in haskell
This is a haskell + networking question... I have a multi threaded haskell server, which accepts client connections, processes their requests, and returns results. It currently works as desired, except where a client drops a connection whilst the server is processing a request. In this circumstance, the server currently doesn't notice that the client has gone until it finishes the processing and attempt to read the next request. I'd like to change it so that the request is aborted as soon as the client disconnects. One way of doing this would would be to maintain a separate thread that is always reading from the client, and buffers until the main thread needs the information. Whilst this would detect the remote close, it also would potentially consume large amounts of memory to maintain this buffer. 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. 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. Thanks, Tim ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] detecting socket closure in haskell
Tim Docker wrote: One way of doing this would would be to maintain a separate thread that is always reading from the client, and buffers until the main thread needs the information. Whilst this would detect the remote close, it also would potentially consume large amounts of memory to maintain this buffer. I think flushing a lost connection causes an error. So instead of buffering its incoming data, you could try flushing the handle and see if that causes an error, which you can then catch. No idea if that will work, but it's worth a try. Martijn. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] detecting socket closure in haskell
Quoth Tim Docker [EMAIL PROTECTED]: | 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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe