c++socketswinsockiocpclosesocket

closesocket() not completing pending operations of IOCP


I am currently working on a server application in C++. My main inspirations are these examples:

Windows SDK IOCP Excample

The I/O Completion Port IPv4/IPv6 Server Program Example

My app is strongly similar to these (socketobj, packageobj, ...).

In general, my app is running without issues. The only things which still causes me troubles are half open connections.

My strategy for this is: I check every connected client in a time period and count an "idle counter" up. If one completion occurs, I reset this timer. If the Idle counter goes too high, I set a boolean to prevent other threads from posting operations, and then call closesocket().

My assumption was that now the socket is closed, the pending operations will complete (maybe not instantly but after a time). This is also the behavior the MSDN documentation is describing (hints, second paragraph). I need this because only after all operations are completed can I free the resources.

Long story short: this is not the case for me. I did some tests with my testclient app and some cout and breakpoint debugging, and discovered that pending operations for closed sockets are not completing (even after waiting 10 min). I also already tried with a shutdown() call before the closesocket(), and both returned no error.

What am I doing wrong? Does this happen to anyone else? Is the MSDN documentation wrong? What are the alternatives?

I am currently thinking of the "linger" functionality, or to cancel every operation explicitly with the CancelIoEx() function

Edit: (thank you for your responses)

Yesterday evening I added a chained list for every sockedobj to hold the per io obj of the pending operations. With this I tried the CancelIOEx() function. The function returned 0 and GetLastError() returned ERROR_NOT_FOUND for most of the operations.

Is it then safe to just free the per Io Obj in this case?

I also discovered, that this is happening more often, when I run my server app and the client app on the same machine. It happens from time to time, that the server is then not able to complete write operations. I thought that this is happening because the client side receive buffer gets to full. (The client side does not stop to receive data!).

Code snipped follows as soon as possible.


Solution