5.13 Exercises

Exercise 5-1. Using the URLConnection techniques demonstrated in Example 5-2, write a program that prints the modification date of a specified URL.

Exercise 5-2. Write a simple server that reports the current time (in textual form) to any client that connects. Use Example 5-8, HttpMirror, as a framework for your server. This server should simply output the current time and close the connection, without reading anything from the client. You need to choose a port number that your service listens on. Use the Connect or GenericClient programs of Examples Example 5-4 and Example 5-5 to connect to this port and test your program.

Exercise 5-3. Write a program that connects to a POP3 mailbox and displays the Subject and From lines of the messages held there. Base your program on the PopClean program of Example 5-7, but be sure to never send the DELE command to delete any messages. A shortcoming of the PopClean program is that it requires you to type your password on the command line, where it can be observed by others in the room. Solve this problem by storing username, password, hostname, and port in an external configuration file that the program reads. See java.util.Properties.load( ) for an easy way to read and parse a configuration file. Alternatively, create a Swing GUI for your program, and use JPasswordField to get the user's password without displaying the characters on the screen.

Exercise 5-4. Write a simple web server that responds to GET requests for local files. You may want to use Example 5-8, HttpMirror, as a framework for your server. Alternatively, you can implement your server as a Service subclass for use with the Server program developed in Example 5-10.

The web server you write should be started with a directory specified on the command line, and it should serve files relative to this directory. When a client requests a file in or beneath the directory, the server should return the contents of the file, but should first output Content-Type, Content-Length, and Last-Modified header lines. For this exercise, assume that files with an extension of .html or .htm have a content type of text/html, and that all other files are text/plain. If a client requests a file that doesn't exist, your server should return an appropriate error code and message.

You may want to use the HttpMirror example to see what a typical HTTP request from a web browser looks like, as your server must be able to handle this kind of request. And you may want to use the HttpClient or GenericClient example to see how existing web servers respond to requests for existing and nonexistent files, since your server should be able to send responses of this sort. Strict conformance to all the details of the HTTP protocol is not a requirement for this exercise, but successful interaction with web browsers and the HttpClient example is.

Exercise 5-5. Modify the UDPSend and UDPReceive programs of Examples Example 5-11 and Example 5-12 so that UDPReceive sends an acknowledgment when it receives a datagram, 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 package, for example, to compute a checksum of the received data and then 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.



Java Examples in a Nutshell
Java Examples in a Nutshell, 3rd Edition
ISBN: 0596006209
EAN: 2147483647
Year: 2003
Pages: 285

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net