This is a very brief guide intended to get you started if you want to program a client/server system under Unix. Note that sockets under windows follow much the same model, but there are subtle differences. If you are interested in writing windows network code, refer to the Winsock API after reading this guide.
Basic Operation
Setting up your client and server to communicate via an internet socket involves fairly stock standard code which you can pretty much copy and paste from here. I have included the example code below, but the basic network system calls look like this:

Concurrent vs Iterative Servers
If you plan on having a single client connect to a single server, then you don’t have to worry about this issue. However, in reality most applications will have multiple clients connecting to a single server. In this case, you have to think about how you design your server in order to handle multiple simultaneous client connections.
The basic choice is to build a concurrent server or an iterative server. In simple terms, a concurrent server handles multiple clients simultaneously, whereas an iterative server handles clients one after the other. Most applications require concurrent servers (in an iterative server, each incoming client connection has to wait until the previous client disconnects before it can connect - not very practical).
The simplest way to create a concurrent server is to fork a new process for every client that connects. This works well if you expect to have a fairly small number of clients connecting at the same time. However, if you expect to have thousands of clients connecting, then it is a very inefficient use of resources to handle each client in a separate Unix process. Hopefully this will not be an issue in your PP assignment, so you can use the simple fork model. Note that other issues such as interprocess communication may be relevant, but are beyond the scope of this little guide. The sample code below is for a concurrent server that forks a new process for each connection.
Sample Code
Rather than me waffling on trying to explain how it all works, I figured I’d just post some sample code. You can pretty much copy and paste this code with slight modifications for your particular application. I have included very verbose comments describing what is happening, hopefully that is enough for you to understand how it all works. If you don’t understand something, feel free to email me, ask me in tutorials, or check out the reference listed in the Further Reading section below.
Note that this code uses a TCP/IP connection and is dependent on the IPv4 protocol. If for some reason you want to use UDP and/or IPv6 etc, you need to read the book listed in Further Reading.
The files
[server.cpp]
This is the code for a simple server that echoes back whatever it receives from the client. Concurrent connections are handled by forking new server processes.
This is the code for a simple client. After connecting it just reads lines from the standard input and sends them to the server. The server’s response is displayed on the standard output.
[sockio.h] [sockio.cpp]
These files have a bunch of #includes that are needed for network programming, and also define readline() and writen() functions that are used for reading/writing from/to sockets (sockets are a bit more delicate than standard file I/O)
After compiling your server and client executables, you can run these both on the same machine… or on different machines, even different countries :) This code will even compile under windows if you use Cygwin, so you can have a cross-platform network system.
A Note on Compiling
When you compile your own programs, you will need to link in the network libraries. To do this, add the following to your g++ command line:
Solaris:
-lnsl -lsocket
Linux:
-lnsl
Further Reading
If you want to learn about network programming under Unix, you must read this book:
Unix Network Programming, by W. Richard Stevens (2nd edition, Prentice-Hall)
It is considered to be The Bible of network programming under Unix. Because Windows follows the same model in its Winsock API (known as the BSD sockets model), it is also very useful to read if you plan to write Windows networking code.
Try and get your hands on the second edition, it has been significantly updated to reflect the changes in the computer world from 1990 - 1997. There are two volumes (I think there were plans for a third, but sadly the author passed away in 1999). You will probably want Volume 1 which explains the basics of sockets. Volume 2 is about Interprocess Communications (IPC).
Last Update: 01/09/2001


Founders at Work: Stories of Startups’ Early Days