Java Reference
In-Depth Information
A Simple Web Server
Example 5-5 shows a very simple web server, HttpMirror . Instead of returning a
requested file, however, this server simply “mirrors” the request back to the client
as its reply. This can be useful when debugging web clients and can be interesting
if you are just curious about the details of HTTP client requests. To run the pro-
gram, you specify the port that it should listen on as an argument. For example, I
can run the server on the host oxymor on.oreilly.com like this:
oxymoron% java com.davidflanagan.examples.net.HttpMirror 4444
Then, in my web browser, I can load http://oxymor on.oreilly.com:4444/test-
ing.html . The server ignores the request for the file testing.html , but it echoes back
the request that my web browser sent. It might look something like this:
GET /testing.html HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/3.01Gold (X11; I; Linux 2.0.18 i486)
Host: 127.0.0.1:4444
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
The main new feature introduced in Example 5-5 is the ServerSocket class. This
class is used by a server, or any other program, that wants to sit and wait for a
connection request from a client. When you create a ServerSocket , you specify
the port to listen on. To connect to a client, call the accept() method of the
ServerSocket . This method blocks until a client attempts to connect to the port
that the ServerSocket is listening on. When such a connection attempt occurs, the
ServerSocket establishes a connection to the client and returns a Socket object
that can communicate with the client. Your code can then call the getInput-
Stream() and getOutputStream() methods of the socket to get streams for reading
bytes from the client and for writing bytes to the client.
Note that the ServerSocket is not used for communication between the server and
its client; it is used only to wait for and establish the connection to the client. Typi-
cally, a single ServerSocket object is used over and over again to establish con-
nections to any number of clients.
Example 5-5 is quite straightforward. It creates a ServerSocket and calls its
accept() method, as outlined previously. When a client connects, it sets up the
streams and then sends some HTTP headers to the client, telling it that the request
has been received successfully and that the reply is text/plain data. Next, it reads
all the HTTP headers of the client's request and sends them back to the client as
the body of its reply. When it reads a blank line from the client, this indicates the
end of the client's headers, so it closes the connection.
Note that the body of the HttpMirror program is a big infinite loop. It connects to
a client, handles the request, and then loops and waits for another client connec-
tion. Although this simple server works perfectly well for the testing purposes for
which it is designed, there is one flaw in it: it is a single-threaded server and can
talk to only one client at a time. Later in this chapter, we'll see examples of servers
that use multiple threads and can maintain connections to any number of clients.
Search WWH ::




Custom Search