[Kc] demonstration of multiple listening with select(2)

david nicol whatever at davidnicol.com
Sun Jan 25 22:51:15 CST 2004


Some days ago I asked kclug if anyone had any examples of
tcp socket code that listens on multiple ports with select.  Nobody
wrote back on that issue, but I did some research and experimenting:
The attached program opens up ports 8021 and 8022 on your machine
and runs a basicly useless service on them both. It handles disconnects
gracefully and will close a connection that issues /quit/.

Enjoy

To make a useful singlethreaded server, you essentially need to
extend the @Something2Say array from MultipleListen into an array
of state objects, still indexed by file descriptor number.  The length
constraints on the syswrite and sysread commands are far far too short,
also -- they are so short to demonstrate that reading and writing over
multiple passes works: when I extend this into an all-in-one e-mail
toaster, the read length will be around 8K, and the write length will be
the length of what is to be written, so the networking layer will take
as much as it can.


I do not know what kinds of situations trigger appearances on the
exception queue. I had thought that connections to listening sockets
would do that, but they appear as read-readiness.  Perhaps sending OOB
data?

David Nicol


-------------- next part --------------
A non-text attachment was scrubbed...
Name: MultipleListen.pl
Type: text/x-perl
Size: 3443 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/kc/attachments/20040126/84d00489/MultipleListen.bin


More information about the kc mailing list