Java Reference
In-Depth Information
5-5. Write a simple web server that responds to
GET
requests like those generated
by the program you wrote in Exercise 5-2. You may want to use Example 5-5
HttpMirror
, as a framework for your server. Alternatively, you can imple-
ment your server as a
Service
subclass for use with the
Server
program
developed in Example 5-9.
Your server should use the HTTP 1.0 protocol or a later version. This means
that the server expects
GET
requests to be followed by header lines and termi-
nated by a blank line. And it means that the responses to
GET
requests
should begin with a version number and status code. This status line is fol-
lowed by header lines, which are terminated with a blank line. The content
of the response follows the blank line. Use the client you wrote in Exercise
5-2 to experiment with existing web servers to see how they respond to vari-
ous
GET
requests.
The web server you write should be started with a directory specified on the
command line and should serve files relative to this directory. When a client
requests a file in or beneath the directory, the server should return the con-
tents of the file, but should first output Content-Type, Content-Length, and
Last-Modified header lines. For this exercise, assume that files with an exten-
sion of
.html
or
.htm
have a content type of
text/html
, and that all other files
are
text/plain
.Ifaclient requests a file that doesn't exist, your server should
return an appropriate error code and message. Again, use the client you
developed in Exercise 5-2 to figure out how existing web servers respond to
requests for nonexistent files.
5-6. Modify the
UDPSend
and
UDPReceive
programs of Example 5-11 and Example
5-12 so that
UDPReceive
sends an acknowledgment when it receives a data-
gram, and so that
UDPSend
doesn't exit until it receives the acknowledgment.
The acknowledgment should itself be a datagram and can contain any data
you desire. (You could use the checksum classes of the
java.util.zip
pack-
age, for example, to compute a checksum of the received data and send this
back in the acknowledgment packet.) Use the
setSoTimeout()
method of
DatagramSocket
so that
UDPSend
doesn't wait for more than a few seconds to
receive the acknowledgment. If the acknowledgment packet is not received
before the timeout,
UDPSend
should assume that the original packet got lost
and was not received. Your modified
UDPSend
should try to resend the packet
once or twice, and if it still doesn't receive an acknowledgment, it should exit
with an error message.