[ale] listen()

Chuck Huber chuck at cehuber.org
Tue Feb 11 13:46:31 EST 2003


On Tue, Feb 11, 2003 at 09:29:16AM -0700, Joe wrote:
> cfowler <cfowler at outpostsentinel.com> writes:
> 
> > I'm writing a server that will only accept one connection at a time.  IT
> > will only service one.  It will then accept a new one after the one
> > being serviced has terminated.  My questions is that listen(int socket,
> > int backlog) does not do this for me.  I'm setting backlog to 0 and I've
> > got clients waiting in the wings to be accept()ed.

That's your problem.  Setting a backlog of zero will cause an error to be
issued to clients trying to connect (resource busy, if I recall correctly).
The value of the backlog passed to listen() is dependent on how fast you
can accept() a connection and how fast you expect connect requests to
arrive balanced with system resources.  A typical value is 5.  

As Joe mentioned, forking a child to accept the connection gets the
parent back to an accept() quicker.

In your single-threaded scenario, there could be a long period before
you arrive back at an accept().  As such, the backlog for listen()
needs to be larger - perhaps 10 - to allow connection requests to be
queued rather than errored.

Enjoy,
    - Chuck
-- 
"The purpose of encryption is to protect good people
from bad people, not to protect bad people from the government."
     Scott McNealy, CEO Sun Microsystems
"The best way for government to control people is to remain in
   a constant threat of war." ---Karl Marx
"They that can give up essential liberty to obtain a little temporary
   safety deserve neither liberty nor safety." -- Benjamin Franklin

 PGP signature




More information about the Ale mailing list