Best way to stop listening on a port.

Hi all, I've been writing some server code like the following: Control.Exception.bracketOnError (listenOn port) sClose procRequests Sometimes I run this server in GHCI and interrupt it with C-c. But when I try and rerun the server it tells me that the port is already bound meaning that sClose either doesn't get called or doesn't complete. Terminating the interpreter seems to work. Is there a better way to correctly stop listening on a port? -deech

Hi,
Could this have something to do with SO_REUSEADDR?
This could be the case if you interrupt the server while some clients
are connected to it.
If so, I think the Haskell way to set this is: setSocketOption socket
ReuseAddr 1
Patrick
On Fri, Jun 4, 2010 at 2:59 PM, aditya siram
Hi all, I've been writing some server code like the following: Control.Exception.bracketOnError (listenOn port) sClose procRequests
Sometimes I run this server in GHCI and interrupt it with C-c. But when I try and rerun the server it tells me that the port is already bound meaning that sClose either doesn't get called or doesn't complete. Terminating the interpreter seems to work.
Is there a better way to correctly stop listening on a port? -deech _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

On Jun 4, 2010, at 14:59 , aditya siram wrote:
Sometimes I run this server in GHCI and interrupt it with C-c. But when I try and rerun the server it tells me that the port is already bound meaning that sClose either doesn't get called or doesn't complete. Terminating the interpreter seems to work.
Most systems keep a port bound for a little while after the socket bound to it exits, so that any stray packets still in flight for that port will correctly be caught and responded to. The usual workaround for this is to set the SO_REUSEADDR flag (which is sO_REUSEADDR in the Haskell network library) so that you can bind to it immediately instead of waiting for it to time out. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (3)
-
aditya siram
-
Brandon S. Allbery KF8NH
-
Patrick LeBoutillier