Posted by Phil Frisbie, Jr. on February 08, 2006 at 15:20:23:
In Reply to: Re: Interrupting a socket waiting on nlAcceptConnection() or nlRead()? posted by David on February 07, 2006 at 20:59:54:
: : This statement confuses me. Why is nlShutdown() not viable? I cannot think of any reason to unblock a single thread unless you are going to shut down the server, and so you would, of course, call nlShutdown().
:
: Because nlShutdown() shuts down your whole network interface. I want to be able to start and stop a server thread independantly of any other connections I might have running. Especially if I have another connection listening on another port.
OK, but let me explain WHY this is a VERY BAD idea.
Internally, sockets are like file handles on most platforms, and are represented by an integer. When you forcibly shut down a socket on one thread while another thread is still listening, there is a possibility that the socket handle might be reused BEFORE the blocking socket call gets the 'invalid socket' error. When this occurs, that thread is now blocking on the WRONG socket!
This REALLY does happen, and will cause you great grief. This is why you should ONLY forcibly shutdown a socket from another thread when you are shutting down the entire server.
: Also, nlThreadSleep() is listed internally- possibly make that public?
You must be using an old version, because this is now exposed in the HawkThreads API.
: One last thing. I tried using nlEnable(NL_BLOCKING_IO) before making a connection, then nlDisable(NL_BLOCKING_IO) after connecting because it's handy to have it just block for connecting. The next time I use nlRead() however, it still blocks. Is this ... not a way I can use these features? [didn't check the error code on nlDisable() yet, but if/why would it fail?]
nlEnable/Disable are global settings that effect the creation of new sockets. You must call nlSetSocketOpt() to directly change a socket's settings.
Phil