Socket Q: Minimum listen queue size 
Author Message
 Socket Q: Minimum listen queue size

This question relates to the uses of sockets for a server and the listen
queue size. I would like to create a socket server with a listen queue
size of 0. i.e. When it is processing 1 connection it will refuse all
other connections (a queue size of 0). The problem is, if you specify 0
as the listen queue size, it actually defaults to SOMAXCON, which is 5
or more depending upon your system. I also tried -1 and it still appears
to default back to SOMAXCON. Is anyone aware of how you can setup a
listen queue size of 0????

Thanks, Andrew.

--
Andrew Wild
Coordinator (Web Technology)
Information Systems Branch
Queensland Department of Environment and Heritage
Australia
Ph: (07) 3227 7991 Fax: (07) 3227 6534
--------------------------------------------------
Visit us online at http://www.*-*-*.com/
--------------------------------------------------



Fri, 03 Aug 2001 03:00:00 GMT  
 Socket Q: Minimum listen queue size
What you probably want is a queue size of one, not zero. That is because
when a connection comes in it gets placed into the queue until you call
accept. If you had a queue of zero then you would never be able to
accept any connections. But the connection is removed from the
queue as soon as you call accept, which means that another could
then be placed into the queue. For what period of time did you want
to refuse connection ?

Quote:

> This question relates to the uses of sockets for a server and the listen
> queue size. I would like to create a socket server with a listen queue
> size of 0. i.e. When it is processing 1 connection it will refuse all
> other connections (a queue size of 0). The problem is, if you specify 0
> as the listen queue size, it actually defaults to SOMAXCON, which is 5
> or more depending upon your system. I also tried -1 and it still appears
> to default back to SOMAXCON. Is anyone aware of how you can setup a
> listen queue size of 0????

--

Life would be so much easier if we could just look at the source code.


Fri, 03 Aug 2001 03:00:00 GMT  
 Socket Q: Minimum listen queue size

[Mailed and posted]

Quote:
> I would like to create a socket server with a listen queue
> size of 0. i.e. When it is processing 1 connection it will refuse all
> other connections (a queue size of 0).

I think your model may be wrong.  Here's what I think happens:
Incoming connections are put in the queue.  When there's an active
`listen' on the socket, the incoming connection is dequeued.  

But if the listen occurs before the connection arrives, the connection
is still queued, and then dequeued immediately.

If this view is correct, then a queue size of 0 doesn't make sense,
because *all* incoming connections would be refused.  I suspect this
is why 0 is forbidden.

On the other hand, a queue size of 1 doesn't seem to be what you
want, because then a new connection might arrive and be queued while
you were processing an old one.

In my experience the best way to deal with this is to write the
program to accept the new connection anyway, but immediately close
it.  For example:

        socket, bind, listen MAINSOCK;

        while (select ...) {
          if (MAINSOCK is ready) {
            if ($BUSY) {      # We're already busy
              # Dispose of excess connection
              accept GO_AWAY, MAINSOCK;
              print GO_AWAY "523 Go away!  I'm busy!\r\n";
              close GO_AWAY;
            } else {
              accept CLIENT, MAINSOCK;
              $BUSY++;  # Don't accept any more new connections
            }
          }

          if (CLIENT is ready) {
            read CLIENT, $buf, 4096;
            if ($buf eq '') {
              close CLIENT;
              $BUSY--;        # Accept new connections now
            } else {
              # process message
            }
          }
        }

I like to design the protocol to support a `go away' message so that
it can take appropriate action.  For example, if it's automated, it
might want to try back in five minutes.  `connection refused' usually
indicates that the server program has stopped running, so checking
back in five minutes is not usually the right thing to do.

Similarly, if the client is interactive, it can give a meaningful
result back to the user.  In your scenario, all they get is
`connection refused' so they don't know if the server is down or not.
In my model, the user gets a message like this:

        % clientprogram your.server.host.name
        Sorry, the server is busy.  Please try back in 17 minutes.



Fri, 03 Aug 2001 03:00:00 GMT  
 Socket Q: Minimum listen queue size
Thanks Graham. I now further understand the model and why at least a
listen queue size of 1 is required. The period of time that I wanted to
refuse the connection was while the current connection was being delt
with and processed. This would normally take about 2-3 seconds.

What would be the idea situation is while process A was being handled,
process B was rejected when it tried to connect to the socket. This
would currently not happen because it would wait in the listen queue
until it was accepted, hence wasteing about 2-3 seconds.

The only way I can think of achieveing some type of outcome like the one
required is to use select (to monitor the file handle) and then fork
when a request comes in. A variable is then set to say 'I am busy'. When
select returns next with a new connect, it sees that the variable 'I am
busy' is set and tells the client to go away. After the forked process
has completed, it changes the 'I am busy' back to 0, hence allowing
clients to be processed once again.....

Does this model sound like it would work???

Cheers, Andrew.

Quote:

> For what period of time did you want
> to refuse connection ?

--
Andrew Wild
Coordinator (Web Technology)
Information Systems Branch
Queensland Department of Environment and Heritage
Ph: (07) 3227 7991 Fax: (07) 3227 6534
--------------------------------------------------
Visit us online at http://www.env.qld.gov.au
--------------------------------------------------


Tue, 07 Aug 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Socket Q: Minimum listen queue size

2. IO::Socket - Listen queue size

3. Listening to STDIN while listening for a socket?

4. Minimum size of Perl-interpreter

5. a minimum size for a dialog box

6. Getting the size of the mail queue.

7. Unable to accept more than one connection on a listening socket

8. PERLFUNC: listen - register your socket as a server

9. PERLFUNC: listen - register your socket as a server

10. IO::Socket: bind or listen errors

11. Listening sockets on Windows

12. 'Listen' option on sockets

 

 
Powered by phpBB® Forum Software