简体   繁体   中英

Make Connect Fail If the server is not waiting in accept call

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM