dis'z mY own kiNda bloG!!!!!

on this blog,you'll find everything and every piece of thing in a different view,any comments are accepted@ my e-mail!

Saturday, September 25, 2010

Client - Server interaction.

  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).
In general, server software has the following characteristics.
  • 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).
  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.
A client and a server
Figure 1: A client and a server
   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.
Two servers
Figure 2: Two servers
   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.

Examples of a Client and a Server

  Figure 3 illustrates the sequence of socket procedure calls required for correct client-server programming.
Socket procedure calls
Figure 3: Socket procedure calls
  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.

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
  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.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 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";

Figure 5: Simple TCP client
  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.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 on penguin 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
It echoes the segments from a client running on 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 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)%

Figure 7: Running client
The text typed by the user is echoed back by the server. The application is terminated by typing end-of-file on standard input.

 This is the way actually a server responds to a negotiation of a client.

Any questions regarding this are entertained.



                                                                                                                                     -kj.,