I have a server socket that I have configured to allow one connection at once (by blocking Accept calls with a semaphore), with a backlog queue size of 1. That is, I called .Listen(1).
I then follow the following process:
- I call AcceptAsync on my server socket (only once)
- I have a client ConnectAsync (connects successfully)
- I have a client ConnectAsync (connects successfully, supposedly in the queue... a way to tell would be nice)
- I have a third client ConnectAsync
These three ConnectAsync calls happen in rapid succession.
The expected result for the third ConnectAsync is to have the SocketAsyncEventArgs "SocketError" property be something other than "SocketError.Success". I actually expect "SocketError.ConnectionRefused", to be specific.
About 95% of the time, this is the case. The third client's callback gives me a SocketError value other than Success.
Every now and then, though, the third ConnectAsync "works", in the same way the second one does. The EventArgs.SocketError gives me SocketError.Success, and the corresponding Socket.Connected property reads "true".
What's going on? I call AcceptAsync exactly once (I have verified this carefully with breakpoints), so only one client should be accepted, and the rest should be on the backlog queue. My queue size is 1, so how is the third client connecting successfully every now and then?
Please don't tell me to use a larger queue size. This is for a test function I've written, and not code that is actively serving clients. At this point it's more curiosity. :)