Winsock2: how to allow ONLY one client connection at a time using listening in VC ++

I want to only allow one connection at a time from my TCP server. Can you please tell me how to use non-zero-length listening.

I am using the code (below), but when I run 2 clients one after the other, both connect. I am using VC ++ with winsock2.

listen (m_socket, -1);

transferring zero as a lag also does not work.

Waiting for an answer to ur.

Regards,
IMMI

+2


source to share


4 answers


If you can really restrict your application to just using Winsock 2, you can use its conditional acceptance mechanism:

SOCKET sd = socket(...);
listen(sd, ...);
DWORD nTrue = 1;
setsockopt(sd, SOL_SOCKET, SO_CONDITIONAL_ACCEPT, (char*)&nTrue, sizeof(nTrue));

      

This changes the behavior of the stack so that it does not automatically send SYN-ACK responses to incoming SYN packets while there is connection space. Instead, your program is signaled to accept the connection as usual - select (), WSAEventSelect (), WSAAsyncSelect () ... - then you call WSAAccept () instead of accept ():



sockaddr_in sin;
WSAAccept(sd, (sockaddr*)&sin, sizeof(sin), ConditionalAcceptChecker, 0);

      

You write the ConditionalAcceptChecker () function to view information about the incoming connection and decide whether to accept the connection. In your case, you can simply return CF_REJECT

while you are already handling the connection.

Again, beware that this mechanism is specific to Winsock 2. If you require portable behavior, it is best to advise other advice on closing the listening socket while your program is already connected.

+6


source


You can set the backlog to 1 as this is the number of connections needed.

But AFAIK there is no hard-and-fast guarantee on the queue size ( this document says it will be a 1.5 * backlog on BSD, for example).



IMHO, you are better off controlling the number of connections manually if you do not accept () connections after some limitation.

+2


source


I would say only accept

once. If you only need one client at a time on your server, you can only use one thread to do the processing. The latency only limits the number of pending connections the system handles to accept (the queue is empty again after the first commit so that the next client gets caught in the backlog), not the number of connections!

+1


source


It's not that much for listening lag.

Listening latency affects the queue used for pending connections and allows the TCP stack to queue pending connections for you.

To do what you want to do, you need to accept one connection that you allow, and then close the listening socket. Once you're done with your only client, you can recreate your listening socket and listen for a new connection. This will prevent more than one client from connecting to you, but the client will not be able to understand what you are actually doing and accepting connections one at a time. All clients, except those who manage to connect, will think that you are simply not there.

This is probably the best design to keep your listening socket open and accept all connections, but once you have one "active" connection you just accept and then send your level message to your client telling him that you cannot accept more or if you can't do it, just close the new connection.

+1


source







All Articles