Sockets and Multiple-connection servers

Lex Spoon lex at cc.gatech.edu
Tue Mar 12 04:21:28 UTC 2002


Derrick Coetzee <dc at moonflare.com> wrote:
> Somewhat a newbie question, somewhat a design question... is there any
> easy way to simply open a server on a port and have it return a Socket
> object for each new connection, each of which can be then be passed on
> to a separate thread for handling as it continues to wait for new
> connections (similar to popular models in Java or MFC Sockets)? Is
> there a good reason not to use this model?

The main issue is that with this style of server socket, there is a
brief period between the time a connection is accepted and when the next
server socket is opened, where no socket is listening.  In that window,
any connection requests that arrive will be refused.  Otherwise it works
fine.  So, you can use it for a telnet server, but not for a WWW server
if you are serving HTML pages with inline images also served from the
same server.  (It's a complex example, but really, the simpler model
goes quite a long way!)

Anyway, a newer model is available nowadays.  If you use
#listenOn:backlogSize: instead of #listen:, then you will be able to use
#accept when a new connection arrives.  #accept, as you would guess from
your knowledge of the BSD sockets API, will create a new socket for the
connection and then keep listening on the old socket.

But really, you should just use ConnectionQueue, which handles all this
for you.  It's really too bad Socket doesn't have any comments that
point you to ConnectionQueue.


-Lex



More information about the Squeak-dev mailing list