I suggest a little bit different solution.
Write a thread that does like this:
// create the socket
// create and associate socket with handle
bool bAcceptingConnections = true;
while( bAcceptingConnections )
{
// ::WSAWaitForMultipleEvents for connection (handle) and exit handle
// was interupt on connection?
{
// accept connection
}
else
{
bAcceptingConnections = false;
}
}
// close listening socket
// close handle
// exit
The exit handle is passed in to this thread or is a global (yuck). Any time
you want to stop accepting connections, signal the exit handle. When you
want to startup again, start the thread again. There's no way you will
accept connections while the socket is closed (just what you want) and
you've just made the cleanest way of creating and closing the socket
possible (it's messy to creat a socket in one line of code, use it in
another, then clean it up in a third).
I use this way with my IOCP socket servers. Works great and is real clean.
You can wrap this thread with a class that has the exit handle and
start/stop functions that check to make sure it's currently running and
signals the exit handle if needed or starts the thread if needed, etc...
makes it real clean. If the listening thread is already running and you call
start( ) it will be smart enough to not start another thread, same if it's
already stopped and you call stop( ).
In my case when I accept the connection I go ahead and create the IOCP
objects and issue the first IOCP reads on the socket and "forget" the new
socket. It will pop up in the IOCP handlers later and there's no need to
track it or even call another function to "hand it off" to some other piece
of code. If it's necessary to send a "hello" or whatever to the client, the
accept code does that too (as long as it's async IO and doesn't require
anything first coming from the client).
Nice eh?
/dev
_______________________________________________
msvc mailing list
[email protected]
See http://beginthread.com/mailman/listinfo/msvc_beginthread.com for
subscription changes, and list archive.