Archived

This topic is now archived and is closed to further replies.

foofightr

Refusing connections

Recommended Posts

Sorta newbie-ish question, but it''s my first time writing network code (using Winsock with pure non-blocking sockets). I want to set a limit on how many players can connect to my server, for example 8 players max. I can keep track of how many are currently connected, that''s easy, but the part that I''m not sure of is what to do when you''re at the max number of clients, and someone else tries to connect. Looking at the error codes returned by connect(), I''d want this someone to receive WSAEREFUSED (connection refused). How do I make sure this person receives this error code when trying to connect to me? Do I simply stop checking for accept() when max # of clients is reached, or do I accept the new connection then close it right away or something? But then this someone''s connect() would succeed and their next recv() would fail, kinda misleading. Should I accept their connection temporarily and send them a packet saying "sorry, game is full" and then boot them? Actually that seems like a reasonable idea. Is there a better way to handle this?

Share this post


Link to post
Share on other sites
Accepting and then closing it right away would send a WSAECONNRESET (10054). What you probably want to do is stop listen()ing on that port, which should result in WSAECONNREFUSED (10061) being sent.

It''s perhaps best to let them connect and receive a ''server full'' message when you can, but if the connections are realling coming in too fast, just stop listening on the port or you''ll be bogged down in sending your refusal messages.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites
One problem to watch out for, at least with regards to TCP, is that closing the listening socket will cause long-delay timeouts on the client (rather annoying if you ask me.) A connect-accept-disconnect is much faster for the client than the client waiting for the connection-request to timeout.

Share this post


Link to post
Share on other sites
> I'd want this someone to receive
> WSAEREFUSED (connection refused).

You can provide a function call to WSAAccept() which will determine if a connection can be accepted or not. Upon rejection, the client would receive a WSAECONNREFUSED from its call to WSAConnect(). The criterion for rejection can be a mix of IP/Port of the source, number of current connections, QoS and network conditions, etc.

-cb


[edited by - cbenoi1 on May 5, 2003 9:35:34 AM]

Share this post


Link to post
Share on other sites