First, I'd recommend a good TCP/IP book. I learned from the
Comer/Stevens series. I would recommend you pick up one of the
following: Internetworking with TCP/IP, Volume III,
BSD version, Windows version,
linux/POSIX version. http://www.bookpool.com is
a good place to get technical books at a discount. Somebody else here
might have some other book recommendations but I've always heard good
reviews about this series.
As far as handling connections goes, you have the following choice:
- Accept a connection, read the request, generate and send the response,
close the socket. This only works for a very lightly loaded server,
small amounts of data, responses that can be generated quickly. This
is the simplest way to put a server together.
- Fork a new process for each connection. On unix-like systems, this
works out well because it doesn't take much to fork. On some other
systems, creating a new process by forking takes a lot of time or
resources and is highly discouraged. Since it sounds like you're
working on Windows, you probably don't want to go this way. This
method works if each client can be handled individually. In your
case, I think your server is going to be a multi-client controller so
your clients need to pass information between them through your
server. If you go the fork route, you'll also be messing with
inter-process communication (IPC) on your server machine to do this.
This takes a lot of coordination (locking, queuing, shared memory,
etc.) You might run into system limits on the number of child
processes or a limit on process per user.
- select() or poll() would allow one process to handle multiple clients
asynchronously. The select() or poll() call is told what handles to
inspect and returns a list of handles that have data ready or are
ready to accept data. This is a fairly efficient way of doing what
you need. Some operating systems, I think Windows included, may have
a limit on the number of network handles you can use with these calls.
Also, some operating systems won't allow you to specify non-socket
handles which could cause problems if you also need to synchronize on
other things. Here you might run into limits on handle counts.
- Create a new thread for each client. This method is similar to the
multi-process model. You still need to figure out how to pass
information between threads and how to synchronize but the solutions
in a threaded model are simpler than in a multi-process model. You'll
have the same handle count limits here as in the select() model.
Which way is best is for you to decide. What works best on one
platform may not work well at all on another. Try to separate your
network interface from the rest of your code so that you can try
implementing it in different ways.
As a disclaimer, I've written many network servers but usually in
C/C++. I've never tried to do one in perl.
---
print map { my ($m)=1<<hex($_)&11?' ':'';
$m.=substr('AHJPacehklnorstu',hex($_),1) }
split //,'2fde0abe76c36c914586c';