001         package com.croftsoft.core.net.http;
002    
003         import java.io.*;
004         import java.net.*;
005         import java.util.*;
006    
007         /*********************************************************************
008         * <P>
009         *
010         * @author
011         *   <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
012         * @version
013         *   2000-04-23
014         *********************************************************************/
015    
016         public class  PrintServer implements Runnable {
017         //////////////////////////////////////////////////////////////////////
018         //////////////////////////////////////////////////////////////////////
019    
020         private static final String  CRLF   = "\r\n";
021    
022         private static final String  SERVER = "FileServer/1.0";
023    
024    
025         private final String        root;
026    
027         private final int           port;
028    
029         private       boolean       running;
030    
031         private       ServerSocket  serverSocket;
032    
033         //////////////////////////////////////////////////////////////////////
034         //////////////////////////////////////////////////////////////////////
035    
036         public static void  main ( String [ ]  args )
037         //////////////////////////////////////////////////////////////////////
038         {
039    System.out.println ( "PrintServer starting..." );
040    
041           String  root = "./";
042    
043           int     port = 80;
044    
045           new Thread ( new PrintServer ( root, port ) ).start ( );
046         }
047    
048         public void  shutdown ( )
049         //////////////////////////////////////////////////////////////////////
050         {
051           running = false;
052    
053           try { serverSocket.close ( ); } catch ( Exception  e ) { }
054         }
055    
056         public  PrintServer ( String  root, int  port )
057         //////////////////////////////////////////////////////////////////////
058         {
059           this.root = root;
060    
061           this.port = port;
062         }
063    
064         public void  run ( )
065         //////////////////////////////////////////////////////////////////////
066         {
067           try
068           {
069             serverSocket = new ServerSocket ( port );
070    
071             running = true;
072    
073             int  index = 0;
074    
075             while ( running )
076             {
077               Socket  clientSocket = serverSocket.accept ( );
078    
079               InputStream  in = clientSocket.getInputStream ( );
080    
081               int  c;
082    
083               while ( ( c = in.read ( ) ) > -1 )
084               {
085                 System.out.print ( ( char ) c );
086               }
087    
088               OutputStream  out = clientSocket.getOutputStream ( );
089    
090    System.out.println ( "writing..." );
091    
092               out.write ( "HTTP/1.0 202\r\n".getBytes ( "US-ASCII" ) );
093    
094    System.out.println ( "flushing..." );
095    
096               out.flush ( );
097    
098    System.out.println ( "closing..." );
099    
100               out.close ( );
101    
102    System.out.println ( "closing socket..." );
103    
104               clientSocket.close ( );
105    
106               index++;
107             }
108           }
109           catch ( Exception  ex )
110           {
111             ex.printStackTrace ( );
112           }
113    
114           try
115           {
116             serverSocket.close ( );
117           }
118           catch ( Exception  e )
119           {
120           }
121         }
122    
123         //////////////////////////////////////////////////////////////////////
124         //////////////////////////////////////////////////////////////////////
125         }