Characteristics of Clients and Servers
In general, client software has the following characteristics.- It is an application program that becomes a client temporarily when remote acces is needed, but performs other computation locally.
- It is invoked by a user and executes for one session.
- It runs locally on the user's computer.
- It actively initiates contact with a server (CONNECT primitive).
- It can access multiple services as needed.(Here we can consider the multiprogramming as discussed in my earlier post).
- It is a special-purpose program dedicated to providing one service.
- It is invoked automatically when a system boots, and continues to execute through many sessions.
- It runs on a shared computer.
- It waits passively for contact from arbitrary remote clients (LISTEN primitive).
- It accepts contact from arbitrary clients, but offers a single service.But,these days server are offering multiple services.(Consider multithreading here).
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.
Figure 1: A client and a server
Figure 2: Two servers
Examples of a Client and a Server
Figure 3 illustrates the sequence of socket procedure calls required for correct client-server programming.Figure 3: Socket procedure calls
Example Server
Figure 4 illustrates a TCP server application written in Perl. The single function call.(Ignore this if you are not familiar with programming,atleast basic knowledge of UNIX is required to understand this concept).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"; |
Figure 4: Simple TCP server
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.But these days servers are capable of receiving multiple incomes and generate multiple outcomes(sending requested data to the clients).Client Example
Figure 5 illustrated a TCP (connection-oriented) client application written in Perl. The single function callIO::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"; |
Figure 5: Simple TCP client
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.But these days,the connection is not terminated until the client or the server intentionally or erroritically disconnects the connection. Running the Server
Figure 6 illustrates the TCP server application running onpenguin
and listening on port 1234.(Penguin is an example of WWW server taken here).penguin(100)% perl tcp-server.pl 1234 Server listening [localhost 35577] Here we go... [localhost 35577] Wow, a response! [localhost 35577] Goodbye penguin(101)% |
Figure 6: Running server
localhost
(i.e. the local machine) using port number 35577. It was then terminated. Running the Client
Figure 7 illustrates the TCP client application contacting a server running onpenguin
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)% |
Figure 7: Running client
This is the way actually a server responds to a negotiation of a client.
Any questions regarding this are entertained.
-kj.,