views:

67

answers:

1

I have a Winsock based server application which uses Windows Winsock I/O Completion Ports.

As such, each connection accepted is associated with the listening socket to start receiving notifications (read, write, close etc').
The listening socket has a backlog of 100 pending connections.
All is good.

At some point I want to stop accepting new connections yet keep communication with already connected existing connected sockets.

I figured I could do one of:

  1. Stop calling WSAAccept().
  2. Set the backlog to zero, effectively disallowing any connection to pend.
  3. Call shutdown() & closesocket() on the listening socket.

Now, option #1 gives the expected results; My application doesn't process new connections, BUT it does accept up to the backlog's amount (100). Connections are practically made - I don't want it!

Option #2; Can I do that? How? Couldn't find on MSDN nor google. The documentation of listen() at MSDN says;

If the listen function is called on an already listening socket, it will return success without changing the value for the backlog parameter. Setting the backlog parameter to 0 in a subsequent call to listen on a listening socket is not considered a proper reset, especially if there are connections on the socket.

Not good for me.
If I could do so in a safe way I would combine it with option #1, effectively really stopping establishing any new connections on the machine (through the listening port!).

Option #3 actually works; After closing the listening socket I can still communicate with existing connections, and the backlog is gone (well, closed the listening socket!).

My concern is that this approach might have some side-effects. Can anyone confirm?

+3  A: 

You can simply close the listening socket. The accepted connections have their own sockets and they will not be affected by closing the listening socket.

For example, in the Microsoft documentation there is a sample server application where basic socket usage is demonstrated. There the listening socket is closed before communication over the accepted socket is done (before the do-while-loop).

sth
Yep, #3 is the right answer.
Steven Sudit
Excellent answer! The "proof" is exactly what I needed for peace of mind!! See, I was afraid the association of the newly accepted conn with the IOCP listening socket would create some dependency. Thank you again!
Poni