This chapter is a précis of Chapter 28 of Doug Comer's book [Comer 2004] and explains the basic client-server model.
In general, client software has the following characteristics.
In general, server software has the following characteristics.
Note that the word server is (strictly) referring to a piece of software. However, a computer running one or more servers is often (incorrectly) called a server.
Like most application programs, a client and a server use a transport protocol to communicate. Figure 1 illustrates a client and a server using the TCP/IP protocol stack.

The client and server each interact with a protocol in the transport layer of the stack. A sufficiently powerful computer can run multiple servers and clients at the same time. Such a computer must have the necessary hardware resources (e.g. a fast CPU and sufficient memory) and have an operating system capable of running multiple applications concurrently (e.g. UNIX or Windows). Figure 2 illustrates such a setup.

The computer in the middle might be running an FTP server and a WWW server. Modern computers are often capable of running many servers at the same time.
Figure 3 illustrates the sequence of socket procedure calls required for correct client-server programming.

Client and server applications can use either connection-oriented or connectionless transport protocols to communicate. High-level languages, such as Perl [Wall 2000] , can hide this low-level code to make network programming less error-prone.
Figure 4 illustrates a TCP server application written in
Perl. The single function call
IO::Socket::INET->new(...) executes the first
four procedure calls shown in Figure 3.
use IO::Socket::INET;
use strict;
my $port = shift
or die"Missing port number\n";
my $socket = IO::Socket::INET->new('LocalPort' => $port,
'Proto' => 'tcp',
'Listen' => SOMAXCONN)
or die "Can't create socket ($!)\n";
print "Server listening\n";
while (my $client = $socket->accept) {
my $name = gethostbyaddr($client->peeraddr, AF_INET);
my $port = $client->peerport;
while (<$client>) {
print "[$name $port] $_";
print $client "$.: $_";
}
close $client
or die "Can't close ($!)\n";
}
die "Can't accept socket ($!)\n";
|
The first two lines specify packages to be used. The next
two lines extract a port number from the command line. The
next four lines perform a passive open with the server
listening for incoming connections. The outer
while loop accepts a connection and computes
the host name and port number of the client. The inner
while loop reads from the client
(<$client>), echoes the data on standard
output (print "[$name $port] $_"), and then
sends the client a copy of its own data, prepended with the
current line number (print $client "$.: $_").
When the client sends end-of-file, the server closes the
current connection and continues the outer loop, waiting for
another incoming connection.
Figure 5 illustrated a TCP (connection-oriented) client
application written in Perl. The single function call
IO::Socket::INET->new(...) executes the first
four procedure calls shown in Figure 3.
use IO::Socket::INET;
use strict;
my $name = shift
or die "Missing server name\n";
my $port = shift
or die "Missing port number\n";
my $socket = IO::Socket::INET->new('PeerAddr' => $name,
'PeerPort' => $port,
'Proto' => 'tcp')
or die "Can't create socket ($!)\n";
print "Client sending\n";
while (<STDIN>) {
print $socket $_;
print scalar <$socket>;
}
close $socket
or die "Can't close socket ($!)\n";
|
The first two lines specify packages to be used. The next
four lines extract a server name and port number from the
command line. The next four lines perform an active open on
the socket between our client and the server. The
while loop reads standard input
(<STDIN>), sends the data to the server
(print $socket $_), and then reads the server's
reply (print scalar <$socket>). At
end-of-file, the connection is closed.
Figure 6 illustrates the TCP server application running on
penguin and listening on port 1234.
penguin(100)% perl tcp-server.pl 1234 Server listening [localhost 35577] Here we go... [localhost 35577] Wow, a response! [localhost 35577] Goodbye penguin(101)% |
It echoes the segments from a client running on
localhost (i.e. the local machine) using port
number 35577. It was then terminated.
Figure 7 illustrates the TCP client application contacting a
server running on penguin and listening on port
1234.
penguin(107)% perl tcp-client.pl localhost 1234 Client sending Here we go... 1: Here we go... Wow, a response! 2: Wow, a response! Goodbye 3: Goodbye penguin(108)% |
The text typed by the user is echoed back by the server. The application is terminated by typing end-of-file on standard input.
| Last modified: Wed Feb 20 13:23:16 2008 |