(1). Close your listen socket. This will not affect currently active sockets. When you decide that you are "not busy", restart your listen socket. In this case, the client can't tell the difference between "server down" and "server busy". The OS will know that no listen socket is bound to the port and should send back to the client "connection refused". Although simple, I don't think this is the best for your situation.
(2). Accept the connection, send back a protocol defined error message and then hang up (close the active socket). In this case, I would not fork a child, just have the main server process send the "not now" message and hang up (close the active socket)
Things to consider: (a) think about how to handle SIGPIPE - normally the main server process doesn't talk to clients, but in this case it would. You should allow for the fact that the write to the active socket could, in a degenerate case trigger a SIGPIPE. (b) Think about what happens if you have some rogue client keeps trying to connect after very short intervals (like 1 ms). (c) Of course you have to decide what "busy" means.
If you are writing both ends (the client and the server) this is the way to go. A very clearly defined and performant action happens when you are "busy". You can put anything in that "I'm busy...go away" message that you want - maybe how many connections you can handle on a "good day", "other url's to go to", it could be anything that you want!
(3). I don't think that moving the listen queue to zero will work. A typical value for this would like 5 or something like that. Anyway, the queue are the other connections rather than the pending one. So a value of zero would still allow a "pending one". You don't want any connections being left unhandled quickly. I don't think that the behavior of this option is very well defined and therefore I believe that it is far inferior to option 2.
Summary: go with option 2 above if you have control of the server and the client. If you do not have control over the client, then consider just accepting the connection and then proceed directly to "hang up", ie. closing the active socket. The client will get a SIGPIPE and know right away that you rejected this connection, well or at least that something didn't work with it and it will know this bit of information very fast.
In reply to Re: TCP server: How to reject connections when busy?
by Marshall
in thread TCP server: How to reject connections when busy?
by squirrel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |