SDSU CS 596 Client-Server Programming
Threads and Servers

[To Lecture Notes Index]
San Diego State University -- This page last updated March 7, 1996
----------

Very Simple Server in Java


This server is copied from the Sun Network tutorial and modified to match the sum server
import java.net.*;
import java.io.*;
import sdsu.io.ASCIIInputStream;

class SumServer 
   {
   static int  portNumber  =  4444;

   public static void main(String args[]) 
      {
      ServerSocket serverSocket = null;

      try 
         {
         serverSocket = new ServerSocket(  portNumber  );
         } 
      catch (IOException e) 
         {
         System.out.println("Could not listen on port: " + 
            portNumber  + ", " + e);
         System.exit(1);
         }

      Socket clientSocket = null;
      try 
         {
         clientSocket = serverSocket.accept();
         } 
      catch (IOException e) 
         {
         System.out.println("Accept failed: " + 
            portNumber  + ", " + e);
         System.exit(1);
         }

      try 
         {
         InputStream  inputSocketStream;
         inputSocketStream  = clientSocket.getInputStream();

         BufferedInputStream  bufferedInputSocketStream;
         bufferedInputSocketStream  = new BufferedInputStream(
                                 inputSocketStream  );

         ASCIIInputStream  cin;
         cin  = new ASCIIInputStream( bufferedInputSocketStream );

         OutputStream  outputSocketStream;
         outputSocketStream  = clientSocket.getOutputStream();

         BufferedOutputStream  bufferedOutputSocketStream;
         bufferedOutputSocketStream  = new BufferedOutputStream(
                              outputSocketStream, 1024  );

         PrintStream  cout;
         cout = new PrintStream( bufferedOutputSocketStream, false);

         cout.print("Enter a number: ");
         cout.flush();
         int one = cin.readInt();

         cout.print("Enter another number: ");
         cout.flush();
         int two = cin.readInt();

         cout.println("The sum is " + (one + two));
         cout.flush();

         cin.close();
         cout.close();
         }
      catch (IOException e)    {  e.printStackTrace(); }
   }
}

Improved Server

Uses methods to show logic
Handles multiple requests ( not at the same time )

import java.net.*;
import java.io.*;
import sdsu.io.ASCIIInputStream;

class SumServer 
   {
   static int  portNumber  =  4444;

   public static void main(String args[]) 
      {
      ServerSocket serverSocket;
      serverSocket = serverSocketOn( portNumber );

      Socket clientSocket = null;
      InputStream  fromClient;
      OutputStream  toClient;

      while ( true )
         {
         clientSocket  =  acceptClientRequestOn( serverSocket );

         fromClient =  inputStreamFromClient( clientSocket  );

         toClient  =  outputStreamToClient( clientSocket  );

         processClientRequest( fromClient, toClient  );
         }
      }


// Here are the methods used
   protected  static  ServerSocket  serverSocketOn( int portNumber )
      {
         ServerSocket serverSocket = null;

      try 
         {
         serverSocket = new ServerSocket( portNumber );
         } 
      catch (IOException e) 
         {
         System.out.println("Could not listen on port: "  + ", " + e);
         System.exit(1);
         }
      return serverSocket;
      }

   protected static Socket acceptClientRequestOn( ServerSocket server)
      {
      Socket clientSocket = null;
      try 
         {
         clientSocket = server.accept();
         } 
      catch (IOException e) 
         {
         System.out.println("Accept failed: "  + ", " + e);
         System.exit(1);
         }
      return clientSocket;
      }


// Here are the methods used Continued

   protected static InputStream inputStreamFromClient( Socket client )
      {
      InputStream  inputSocketStream;
      BufferedInputStream  bufferedInputSocketStream = null;

      try 
         {
         inputSocketStream  = client.getInputStream();

         bufferedInputSocketStream  = new BufferedInputStream(
                                 inputSocketStream  );

         }
      catch  ( IOException e)
         {
         // What to do here?
         }
         return  bufferedInputSocketStream;
      }


// Here are the methods used Continued


   protected static OutputStream outputStreamToClient( Socket client )
      {
      OutputStream  outputSocketStream;
      BufferedOutputStream  bufferedOutputSocketStream = null;

      try
         {
         outputSocketStream  = client.getOutputStream();

         bufferedOutputSocketStream  = new BufferedOutputStream(
                           outputSocketStream, 1024  );
         }
      catch  ( IOException e)
         {
         // What to do here?
         }
      return  bufferedOutputSocketStream;
      }


// Here are the methods used Continued

   protected static void processClientRequest( InputStream in, 
                                    OutputStream out )
      {
      try
         {
         ASCIIInputStream  cin  =  new ASCIIInputStream( in );
         PrintStream  cout          =  new PrintStream( out );

         cout.print("Enter a number: ");
         cout.flush();
         int one = cin.readInt();

         cout.print("Enter another number: ");
         cout.flush();
         int two = cin.readInt();

         cout.println("The sum is " + (one + two));
         cout.flush();

         cin.close();
         cout.close();
         }

      catch (IOException e) 
         {
         e.printStackTrace();
         }
      }
   }

Using Threads in a Concurrent Server

class SumHandler implements Runnable
   {
   protected ASCIIInputStream  cin;
   protected PrintStream  cout;

   public  SumHandler( InputStream in, OutputStream out )
      {
      cin     =  new ASCIIInputStream( in );
      cout   =  new PrintStream( out, false);
      }

   public void run ()
      {
      try
         {

         cout.print("Enter a number: ");
         cout.flush();
         int one = cin.readInt();

         cout.print("Enter another number: ");
         cout.flush();
         int two = cin.readInt();

         cout.println("The sum is " + (one + two));
         cout.flush();

         cin.close();
         cout.close();
         }
      catch (IOException e) 
         {  // What to do here? }
      }
   }

Changes to SumServer

The processClientRequest method needs to change
All other aspects of previous SumServer stay the same.

New version of processClientRequest

   protected static void processClientRequest( InputStream in, 
                                    OutputStream out )
      {
      SumHandler  adder  =  new  SumHandler( in, out);
      Thread  serverThread  =  new  Thread( adder, "Add" );
      serverThread.start();
      }

----------