1 // File: GenericServer.java
2 // Author: Thomas Lea (leat@goodnet.com, http://www.goodnet.com/~leat)
4 // Version: 1.0 (for Beta JDK API)
6 // Copyright (c) 1995 Thomas Lea. All Rights Reserved.
10 // Please feel free to take this program and modify it to your hearts content.
11 // The only thing I ask is that my name and the above version/copyright
12 // information stay at the top of this module.
16 import java.util.Vector;
20 private static final int DEFAULT_PORT=8887;
21 private ConnectionManager cm = null;
23 public GenericServer(int port)
25 System.out.println("Server is initializing to port " + port);
27 cm = new ConnectionManager(port);
31 public static void main(String args[])
37 server_port = Integer.parseInt(args[0],10);
41 System.out.println("Defaulting to port " + DEFAULT_PORT);
42 server_port = DEFAULT_PORT;
44 new GenericServer(server_port);
48 // Waits for a connection then spawns a ServerConnection to deal with it
49 class ConnectionManager extends Thread
51 private static int _port;
52 private static Vector _my_threads = new Vector(5,2);
53 //size of 5 initially, grow by 2
54 private ServerSocket _main_socket = null;
56 public ConnectionManager(int port)
66 private void serveRequests()
68 try {_main_socket = new ServerSocket(_port);}
69 catch(Exception e) { System.err.println(e); System.exit(1);}
71 ServerConnection temp_sc = null;
77 Socket this_connection = _main_socket.accept();
79 temp_sc = new ServerConnection(this_connection);
81 _my_threads.addElement(temp_sc);
83 //clean up the vector if needed
84 for(int i=0;i<ConnectionManager._my_threads.size();i++)
85 if(!((ServerConnection)(_my_threads.elementAt(i))).isAlive())
86 _my_threads.removeElementAt(i);
90 System.err.println("Exception:\n" + e);
96 class ServerConnection extends Thread
98 private Socket _mysocket;
99 private PrintStream _output;
100 private InputStream _input;
101 FileInputStream _dis;
103 public ServerConnection(Socket s)
108 private void doServerWork()
110 //This is where the server actually does its work.
111 //when this method finishes, the socket will be closed
112 //and this thread will exit.
114 //This is just some junk... put your real work here.
117 byte b[] = new byte[1024];
118 int nbytes = _input.read(b,0,1024);
119 String str = new String(b,0,0,nbytes);
121 _dis = new FileInputStream("FILE.HTM");
122 StringBuffer buf = new StringBuffer(4096);
124 System.out.println("Received for server: " + str);
126 nbytes = _dis.available();
127 _output.println("HTTP/1.0 200 OK");
128 _output.println("MIME-Version: 1.0");
129 _output.println("Date: 22Dec95");
130 _output.println("Server: Java_Server_0.01");
131 _output.println("Content-type: text/html");
132 // _output.println("Content-Length: 76");
134 // _output.println("<HEAD><TITLE>Clock 2.0 Title</TITLE></HEAD><BODY>");
135 // _output.println("<H1>Clock 2.0</H1>");
136 b = new byte[nbytes];
137 _dis.read(b, 0, nbytes);
138 str = new String(b, 0, 0, nbytes);
139 _output.println(str);
140 // _output.println("</BODY>");
142 // for(int i=0;i<10;i++)
144 // _output.println("This is a message from the server");
145 // sleep((int)(Math.random() * 4000));
155 System.out.println("Connected to: " +
156 _mysocket.getInetAddress() +":"+ _mysocket.getPort());
159 _output = new PrintStream(_mysocket.getOutputStream());
160 _input = _mysocket.getInputStream();
165 //We are outta here....
168 catch ( Exception e )
170 System.err.println( "Exception:\n" + e );
173 System.out.println("Disconnecting: " + _mysocket.toString());