WebServer0public class WebServer0 extends Object A very very very simple Web server.
There is only one response to all requests, and it's hard-coded.
This version is not threaded and doesn't do very much.
Really just a proof of concept.
However, it is still useful on notebooks in case somebody connects
to you on the Web port by accident (or otherwise).
Can't claim to be fully standards-conforming, but has been
tested with Netscape Communicator and with the Lynx text browser. |
Fields Summary |
---|
public static final int | HTTP | ServerSocket | s |
Constructors Summary |
---|
WebServer0()Constructor, just create the server socket.
try {
s = new ServerSocket(HTTP);
} catch(IOException e) {
System.err.println(e);
System.exit(0);
return;
}
|
Methods Summary |
---|
public void | Handler(java.net.Socket s)Handler() handles one conversation with a Web client.
This is the only part of the program that "knows" HTTP.
BufferedReader is; // inputStream, from Viewer
PrintWriter os; // outputStream, to Viewer
String request; // what Viewer sends us.
try {
String from = s.getInetAddress().toString();
System.out.println("Accepted connection from " + from);
is = new BufferedReader(new InputStreamReader(s.getInputStream()));
request = is.readLine();
// TODO new StreamTokenizer(request); to parse it
System.out.println("Request: " + request);
String nullLine = is.readLine();
os = new PrintWriter(s.getOutputStream(), true);
os.println("HTTP/1.0 200 Here is your data");
os.println("Content-type: text/html");
os.println("Server-name: Java WebServer 0");
String reply = "<HTML><HEAD>" +
"<TITLE>Wrong System Reached</TITLE></HEAD>\n" +
"<H1>Welcome, " + from + ", but...</H1>\n" +
"<P>You have reached a desktop machine " +
"that does not run a real Web service.\n" +
"<P>Please pick another system!\n" +
"<P>Or view <A HREF=\"file:///C:/javasrc/WebServer0.java\">" +
"the WebServer0 source (at a Course 47x site only)</A> " +
"or <A HREF=\"http://www.darwinsys.com/java/server.html\">" +
" at the Course Authors Web Site</A>.\n" +
"<HR><I>Java-based WebServer0</I><HR>\n" +
"</HTML>\n";
os.println("Content-length: " + reply.length());
os.println("");
os.println(reply);
os.flush();
s.close();
} catch (IOException e) {
System.out.println("IOException " + e);
}
return;
| public static void | main(java.lang.String[] argv)Main method, just creates a server and call its runServer().
System.out.println("DarwinSys JavaWeb Server 0.0 starting...");
WebServer0 w = new WebServer0();
w.runServer(); // never returns!!
| public void | runServer()RunServer accepts connections and passes each one to handler.
Socket us; // user socket, gotten from accept()
while (true) {
try {
us = s.accept();
} catch(IOException e) {
System.err.println(e);
System.exit(0);
return;
}
Handler(us);
}
|
|