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:

client.gif server.gif

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.

[client.cpp]

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).


Copyright (c) 2001, Ryan Junee
Last Update: 01/09/2001
Digg ThisPowered by Gregarious

Upcoming Events

(full list)
  • No events.
July 2008
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
28293031EC

Bookshelf


Currently Reading:
Recently Finished:
Founders at Work: Stories of Startups’ Early DaysFounders at Work: Stories of Startups’ Early Days
by Jessica Livingston

Fascinating stories of start-ups from ground zero - RJ

iWoz: From Computer Geek to Cult Icon: How I Invented the Personal Computer, Co-Founded Apple, and Had Fun Doing ItiWoz: From Computer Geek to Cult Icon: How I Invented the Personal Computer, Co-Founded Apple, and Had Fun Doing It
by Steve Wozniak

Proof that the some of the worlds best innovations are designed by one person, with a love of technology. - RJ

Atlas ShruggedAtlas Shrugged
by Ayn Rand

A life changing study of philosophy, politics, business sex and power. The capitalists’ bible. - RJ

Stick and Rudder: An Explanation of the Art of FlyingStick and Rudder: An Explanation of the Art of Flying
by Wolfgang Langewiesche

Must read if you want to grok how to control your airplane -RJ

Yahoo! HacksYahoo! Hacks
by Paul Bausch

Some useful tidbits if you want to create mashups using Yahoo! services -RJ

The Monk and the Riddle: The Art of Creating a Life While Making a LivingThe Monk and the Riddle: The Art of Creating a Life While Making a Living
by Randy Komisar

Entertaining and insghtful study of the reasons for starting a business -RJ

The Art of Innovation: Lessons in Creativity from IDEO, America’s Leading Design FirmThe Art of Innovation: Lessons in Creativity from IDEO, America’s Leading Design Firm
by Tom Kelley

Insider’s view of a creative and fun organization - ideas you can replicate -RJ

The FountainheadThe Fountainhead
by Ayn Rand

Thought-provoking character study of selfishness vs selflessness, and the power of the individual -RJ


Close
E-mail It