Reputation: 5203
I need to create a server socket, which should reject a connect request, if it is not blocking at the accept call.
The behavior of the server will be
a. wait for a client to connect
b. wait till the client closes the connection
c. Goto step a
if the server is waiting for the client connection to close, it should reject a client connect call immediately. We cannot close the server socket after getting a client connection.
Is that possible?
Upvotes: 0
Views: 325
Reputation: 595367
What you ask for simply not possible on the server side. There is simply no way to reject a pending connection while it is still in the queue waiting to be accepted. You can (and should) set the backlog value to 1 when calling listen()
, but that just means there can be at most 1 connection in the queue while you are actively communicating with another client. You would have to call accept()
to remove that pending connection from the queue and then call closesocket()
on it immediately if you don't want to talk to that client at that time.
What you can do instead is call WSAAccept()
instead of accept()
so you can assign a callback function that returns CF_REJECT
or CF_ACCEPT
as needed, and then you need to continuously call WSAAccept()
, either in a thread or with overlapped I/O, to remove pending connections from the queue and reject them while you are communicating with an accepted connection until you close it.
Upvotes: 1