SDSU CS 596: Client-Server Programming
Spring Semester, 1997
Doc 29 CGI - Server Example

To Lecture Notes Index
San Diego State University -- This page last updated Apr 29, 1997

Contents of Doc 29 CGI - Server Example

import java.util.*;
import sdsu.io.*;
import java.io.*;

class Echo
     {
     private ASCIIInputStream cin;
     private PrintStream cout;

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

     public void run() throws IOException
          {
          String fromClient = cin.readLine().trim();
          cout.println( "You entered: " + fromClient  );
          }
     }


class CGIStreamExample 
     {
     public static void main( String args[] ) throws IOException {
          try
               {
               preformTest();
               }
          catch ( Exception anError )     {
               System.out.println( "Content-type: text/plain\n\n");
               System.out.println( "A java exception occured. Here it is:");
               System.out.println( anError.toString() );
               }
          }
          
     public static void preformTest() throws IOException
          {
          PipedInputStream myIn = new PipedInputStream();
          PipedOutputStream myOut = new PipedOutputStream();
          PipedInputStream echoIn = new PipedInputStream();
          PipedOutputStream echoOut = new PipedOutputStream();

          System.out.println( "Content-type: text/html\n\n");

          echoIn.connect( myOut );
          myIn.connect( echoOut );
          
          PrintStream cout = new PrintStream( myOut );
          ASCIIInputStream cin = new ASCIIInputStream( 
                                        new BufferedInputStream(myIn));

          Echo repeater = new Echo( echoIn, echoOut );

          cout.println("Hello World");
          repeater.run();
          String result = cin.readLine();

          System.out.println( "The result is: " + result );
          }
     }