SDSU CS 696 Emerging Technologies: Distributed Objects
Spring Semester, 1998
Basic Client-Server

To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 21-Apr-98

Contents of Doc 3, Basic Client-Server

    1. Definitions
    2. Basic Mechanics of Client-Server
    3. Issues
      1. Concurrency
      2. Protocol
      3. Error Checking
      4. Logging
      5. Stateful vs Stateless Servers
References

CS 580 Lecture Notes at:
http://ww.eli.sdsu.edu/courses/spring97/cs596/notes/index.html
http://ww.eli.sdsu.edu/courses/spring96/cs596/notes/index.html
http://ww.eli.sdsu.edu/courses/spring95/cs596_3/notes/index.html

Doc 3, Basic Client-Server Slide # 2
Basic Client-Server

Definitions


Client - one that makes a request of another

Server - the receiver of the request

Client-Server in Objects
class foo
   {
   bar server = new bar();
   
   public void makeRequest()
      {
      server.request();
      }
   }

class bar
   {
   public String request()
      {
      return "The answer is no";
      }
   }


Doc 3, Basic Client-Server Slide # 3
Definitions Continued

Client-Server Programs

A program on machine A can be a server to program on other machine


Client-Server Machines
Sometimes the machines running the server (client) program is called the server (client)


Remote Client-Server Objects
In distributed objects system an object on one machine can be a server to an object on another machine


Doc 3, Basic Client-Server Slide # 4

Basic Mechanics of Client-Server

There is no Magic!

For a client to connect to a server program:
the server program must already running
the server program must be listening to a port for incoming connections
the client must know the server host machine and the port the server using


Doc 3, Basic Client-Server Slide # 5
A Java Server
import java.net.*;
import java.io.*;
import java.util.*;

class JustADemoServer
   {
   public static void main(String[] args ) throws Exception
      {
      ServerSocket acceptor = new ServerSocket( 0 );
      System.out.println("Using port " + acceptor.getLocalPort());
      
      while (true)
         {
         Socket client = acceptor.accept();
         
         InputStream rawInput = client.getInputStream();
         BufferedReader input = new BufferedReader(
                           new InputStreamReader( rawInput) );
         
         OutputStream rawOutput =  client.getOutputStream();
         PrintWriter      output = new PrintWriter( rawOutput );

         String inputLine = input.readLine().toLowerCase();
         
         if ( inputLine.startsWith("date") )
            {
            Date now = new Date();
            output.println(now.toString());
            output.flush();
            }
            
         input.close();
         output.close();  
         }
      }
   }

Doc 3, Basic Client-Server Slide # 6
A Java Client
import java.net.*;
import java.io.*;
import sdsu.util.ProgramProperties;

public class GetDate
   {
   public static void main(String[] args) throws Exception
      {
      ProgramProperties commandLine = 
         new ProgramProperties( args);

      String host = commandLine.getString( "h" );
      int port = commandLine.getInt( "p" );

      Socket   server = new Socket( host, port);

      InputStream rawInput = server.getInputStream();
      BufferedReader input = new BufferedReader(
                           new InputStreamReader( rawInput) );
      
      OutputStream rawOutput =  server.getOutputStream();
      PrintWriter      output = new PrintWriter( rawOutput );

      output.println( "Date" );
      output.flush();

      System.out.println(inputLine.read());

      output.close();
      input.close();
      }
   }

Doc 3, Basic Client-Server Slide # 7
For those without Client-Server Experience

1. Run JustADemoServer

2. From a different machine telnet to the machine running the server and to the port the server is using, then type Date followed by a return


From a UNIX machine this can be done with:
telnet host port

3. Run the GetDate program giving it the proper host and port number, for example:
java GetDate -h=rohan.sdsu.edu -p=45232


Doc 3, Basic Client-Server Slide # 8

Issues

Concurrency


The JustADemoServer handles only one client at a time

What if:
A server "long time" to compute the answer for the client
Many clients connect to the server in a short time
Iterative Server

Handles one request at a time

Use when requests are guaranteed to be completed within a small amount of time.

Advantage:
Trivial to implement.
Easy to serialize accesses to a central database.

Problems:
Server is locked while dealing with a request. If the request does take longer than allowed, no other clients can get service.


Doc 3, Basic Client-Server Slide # 9
Concurrent Server

Handles many requests at the same time

Use where the time taken to complete a request cannot be limited.

Advantages:
Individual client requests can be of any complexity and duration

Problems:
Complexity inherent in concurrent processing.


Doc 3, Basic Client-Server Slide # 10

Protocol


Protocol
Set of rules and conventions used by communicating participants

Protocol Requirements
Well defined ( Protocol can not be vague )
Complete ( All situation must be addressed )
Computer program must be able to parse protocol
Extendible


Protocols are an very important (most important) part of client-server programming

The protocol used by JustADemoServer is not well defined nor complete

GetDate running on a Mac or PC will not work with a JustADemoServer running on a UNIX machine

Doc 3, Basic Client-Server Slide # 11

Error Checking


The following client will crash the server
import java.net.*;
import java.io.*;
import sdsu.util.ProgramProperties;

public class GetDate
   {
   public static void main(String[] args) throws Exception
      {
      ProgramProperties commandLine = 
         new ProgramProperties( args);

      String host = commandLine.getString( "h" );
      int port = commandLine.getInt( "p" );

      Socket   server = new Socket( host, port);

      InputStream rawInput = server.getInputStream();
      
      OutputStream rawOutput =  server.getOutputStream();

      rawInput .close();
      rawOutput .close();
      }
   }


Doc 3, Basic Client-Server Slide # 12

Logging


How many people use your server?

How will you know if someone is trying to hack into your server?
How will you know what they did?

If something goes wrong in the server how will you know? How will you determine what went wrong?

How will you know if a particular client program has trouble connecting to your server?

Servers need to log client access
Security

Who should have access to your server?

What should individual be limited to doing on your server?

If the traffic between client and server secure?

Who validates a user?

Doc 3, Basic Client-Server Slide # 13

Stateful vs Stateless Servers


Stateless Server:
Client connects to the server
Client makes one request
Server handles request
Client and server drop connection

Stateful Server
Client connects to the server
Client makes one request
Server handles request
Client makes another request
Server handles request
etc.
Client and server drop connection when done
Server needs to remember information about the client while the client is connected - the state of the connection
Stateful servers are much more complicated than stateless servers




visitors since 29-Jan-98