Allen Rouse asks " What books are there that deal exclusively with the issues of writing servers? For example, Richard Stevens' books deal with the tools needed to write any kind of UNIX software, servers are just one among many. He mentions general TYPES of servers but not much about the specific issues encountered in writing complex servers (like say NetTrek or an ORB)."
Stallman is the starting point
Specifically, the book series
"Unix Network Programming" is a very very good place to start. Currently, only the 1st volume of the series is available, but it is offers a very in depth view of what to do to write a server (and clients, etc) and why. Unfortunately, Stallman has a nasty tendancy to make you read through his entire book to understand a more advanced concept, so you can't just 'flip' to a later topic and pick up there without at least some going back of chapters to see how he explains things... even if you already have an idea of what he's trying to say.
If you're like me, you probably want to immediately leap into an operational code situation right away. There's a number of FAQs online about quick 'n dirty client server, but a suprisingly straightfoward c-s demonstration is available in the Perl manual, by typing man perlipc. That's where I got my start.
Hmm... well, it depends on what you want to serve. And how you want to transmit information. You can write "server" shell scripts, and run them from inetd, and they will use their standard in/standard out. A simple web "server", for instance, could be
HTTP/1.0 200 OK
echo Content-type text/html
echo '<html><head><title>Hello!</title></head><body>Hello, World! You said:'"`cat`"'</body></html>'
run from port 80.
Now, if you want a daemon, I don't remember... you have to fork, cut off stdin/out/err, and do a couple of other things...
Also, remember that this server should be secure. Keep away from possible buffer overruns. Stuff like that.
Secure programming hints
For servers, it's important to start thinking in terms of security as early as possible.
Here are some nice resources I've found that deal with secure programming:
- Designing secure software
- Writing Safe Setuid Programs
- Security Code Review Guidelines
- secure programming checklist
Perl? Try NetServer::Generic
Just a quick plug for NetServer::Generic, a perl module intended to make writing TCP/IP servers easy.
Version 0.2 should show up on CPAN in a week or so (when I finish jumping up and down on it and write some installation tests and documentation).
Entire Book on writing servers is overkill!
While it certainly doesn't seem that way
initially, it's a very steep learning curve
which becomes almost horizontal after the first
few feet. (insert other annoying analogies at
Servers require a several page magazine article
at best (The Linux Journal had one in fact :)
Things you must learn, not entirely server
related, but the sum of these make whole:
* Sockets. Without sockets, you may as well not
have a computer. You can find a fairly neat
sockets programming tutorial at
* UNIX programming. (duh). Seriously, UNIX
programming is more than just knowing C. UNIX
programming manuals will help with this quite
wonderfully (obviously :).
* Forking/Threading. You DO want your server to
handle more than one connection at a time, don't
you? Forking is part of UNIX programming lore, but
threading is newer, less-standardized, and
* RFC. The Sacred RFC's define much of our lives.
If you're looking to write a server, search an
RFC Index for the server in question, download the
RFC, and assimilate it. They're rather cryptic
and sterile in nature, but you get used to it
after you read a hundred or so. :)
Probably the coolest way of writing a server if
you don't want to sweat all of the sockets/forking
details is by making use of inetd. (man inetd).
inetd can basically let IDIOTS (no offense :)
write functional servers. (man inetd.conf)
Basically this happens:
When something connects to your system, inetd
will see if it has a servlet to handle it, if it
does, it starts the servlet and sends whatever it
recieves on a socket to the servlet's standard
input, and whatever the servlet writes to standard
output is sent to the remote client over the
network. Neat huh?
As for complex servers, really complicated servers
are just simple servers with complicated features.
(I can probably say dumber things today, wait)
There's really not much to learn. Now the data
you want to send back to a client that connects
to your server is a different story, but it
itself isn't much a server thing at all. :)
Use the source
If you want to know how to write a server, find a package that you think was written around similar principles as what you expect yours to be like, and which you like to use, and start examining what was done in that program, and why.
If you want a fast and secure network server, look at the design of qmail, and ask about the design choices on the qmail list. If you want to know more about something like a MUSH, then find your favorite mush server, and talk to the folks who wrote the server, and those who designed the world. Etc., etc.
It's also very instructional to find out what specific changes were made in different versions of the same program, or in competing programs.
Re: Writing a server?
I would suggest UNIX Distributed Programming by Chris Brown.
Prentice Hall Press; ISBN: 0130758965
This is a well written book with tons of working code. It will definitely get you going in the right direction.
reference for writing a (web) server
If you are interested in writing a Web server or
an application that uses the HTTP protocols),
have a look at the book "Illustrated Guide to HTTP" by Paul Hethmon. Unfortunately he doesn't
use Unix/Linux (although the code examples are
written in C/C++) but the book does cover how to
use the HTTP/1.1 protocols. Then you could
have a look at Apache and Jigsaw to see how
they are implemented.
Internetworking w/ TCP/IP Vol III
THE book you are looking for is Internetworking w/ TCP/IP Vol. III. by Comer and Stevens. (2nd edition). It has everything you'll need to develop clients and servers for BSD socket based systems. Here is a short chapter listing:
1. Introduction and Overview
2. The Client Server Model and Software Design
3. Concurrent Processing in Client-Server Software
4. Program Interface to Protocols
5. The Socket Interface
6. Algorithms and Issues in Client Software Design
7. Example Client Software
8. Algorithms and Issues in Server Software Design
9. Iterative, Connectionless Servers (UDP)
10. Iterative, Connection-Oriented Servers (TCP)
11. Concurrent, Connection-Oriented Servers(TCP)
12. Single-Process, Concurrent Servers (TCP)
13. Multiprotocol Servers (TCP, UDP)
14. Multiservice Servers (TCP,UDP)
15. Uniform, Effiecient Management of Server Concurrency
16. Concurrency in Clients
17. Tunneling At the Transport And Application Levels
18. Application Level Gateways
19. External Data Representation (XDR)
20. Remote Procedural Call Concept (RPC)
Ugh, there are 8 more chapters.. but you get the idea. Lots of GREAT examples, easy to understand. I use this book all the time.
OO Framework for building communication SW...
Here's for those who won't make it to the web site :
Overview of ACE
The ADAPTIVE Communication Environment (ACE) is an object-oriented (OO) framework that implements many core
design patterns for concurrent communication software. ACE provides a rich set of reusable C++ wrappers and
framework components that perform common communication software tasks across a range of OS platforms. The
communication software tasks provided by ACE include event demultiplexing and event handler dispatching, signal
handling, service initialization, interprocess communication, shared memory management, message routing, dynamic
(re)configuration of distributed services, concurrent execution and synchronization.
ACE is targeted for developers of high-performance and real-time communication services and applications. It
simplifies the development of OO network applications and services that utilize interprocess communication, event
demultiplexing, explicit dynamic linking, and concurrency. In addition, ACE automates system configuration and
reconfiguration by dynamically linking services into applications at run-time and executing these services in one or
more processes or threads.
ACE has been ported and tested on a wide range of OS platforms including Win32 (i.e., WinNT 3.5.x, 4.x, Win95, and
WinCE using MSVC++ and Borland C++ on Intel and Alpha platforms), most versions of UNIX (e.g., Solaris 1.x and 2.x on
SPARC and Intel, SGI IRIX 5.x and 6.x, DG/UX, HP-UX 9.x, 10.x, and 11.x, DEC UNIX 3.x and 4.x, AIX 3.x and 4.x, DG/UX,
UnixWare, SCO, and freely available UNIX implementations like Linux, FreeBSD, and NetBSD), real-time operating
systems (e.g., LynxOS, VxWorks, and PSoS), and MVS OpenEdition. A single source tree is used for all these platforms.
There is also a Java version of ACE.