Java Reference
In-Depth Information
* @param port The port to use for the server.
* @throws IOException Thrown if any sort of error occurs.
*/
public SimpleWebServer(int port) throws IOException
{
serverSocket = new ServerSocket(port);
}
/**
* The run method endlessly waits for connections.
* As each connection is opened (from web browsers)
* the connection is passed off to handleClientSession.
*/
public void run()
{
for (;;)
{
try
{
Socket clientSocket = serverSocket.accept();
handleClientSession(clientSocket);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
* Handle a client session. This method displays the incoming
* HTTP request and responds with a "Hello World" response.
*
* @param url The URL to download.
* @return The contents of the URL that were downloaded.
* @throws IOException Thrown if any sort of error occurs.
*/
private void handleClientSession(Socket socket)
throws IOException
{
// setup to read from the socket in lines
InputStream is = socket.getInputStream();
InputStreamReader inputStreamReader =
new InputStreamReader(is);
BufferedReader in = new BufferedReader(inputStreamReader);
// setup to write to socket in lines
Search WWH ::




Custom Search