Methods Summary |
---|
protected void | buildGUI()
JPanel newContentPane = new JPanel(new BorderLayout());
newContentPane.add(buildMainPanel(), BorderLayout.CENTER);
setContentPane(newContentPane);
setSize(250, 300);
|
private javax.swing.JComponent | buildMainPanel()
_displayArea = new JTextArea();
_displayArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(_displayArea);
scrollPane.setBorder(new EtchedBorder());
return scrollPane;
|
private void | processClientRequest(java.net.Socket client)
_displayArea.append("Client connected from port " +
client.getPort() + " on machine " + client.getInetAddress() + "\n");
_displayArea.append("Request is: \n");
readRequest(client);
sendResponse(client);
client.close();
|
private void | readRequest(java.net.Socket client)
BufferedReader request = null;
request = new BufferedReader(new InputStreamReader(client.getInputStream()));
String nextLine;
while (null != (nextLine = request.readLine())) {
// Ideally, we'd do something. But this is a very
// simple web server.
if (nextLine.equals("")) {
break;
} else {
_displayArea.append("\t" + nextLine + "\n");
}
}
_displayArea.append("---------------------------------------\n");
return;
|
private void | sendResponse(java.net.Socket client)
BufferedWriter response;
response = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
response.write(_mainPage);
response.flush();
|
public void | startListening()
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(80);
} catch (IOException e) {
return;
}
while (true) {
try {
Socket client = serverSocket.accept(); // wait here
processClientRequest(client);
//bad design-- should handle requests in separate threads
// and immediately resume listening for connections client.close();
} catch (IOException e) {
}
}
|