Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winsock - stop accepting new connections yet keep comm with existing connections

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?

like image 701
Poni Avatar asked Aug 09 '10 21:08

Poni


1 Answers

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).

like image 117
sth Avatar answered Nov 06 '22 15:11

sth