Thursday, April 6, 2006
Due: Wednesday, April 12, 2006
Work on as much of this lab as you can today; we will continue on this material in class tomorrow as well.
Internet Application Level Protocols
Reading
Read over Section 24.1 (pages 906-909) of the textbook. If you do not have the expanded version of the book, I will give you copies of these pages.
Connecting to a Web Server Using Telnet
Read the first page of section 24.2 (page 910).
The telnet program enables a user to type characters to be sent to a remote computer, and view characters that the remote computer sends back. Login to the CS server using the SSH client (the SSH client itself is a newer, secure alternative to telnet). At the command line in the SSH terminal, type:
telnet java.sun.com 80
Once the program starts, type very carefully, without making any typing errors and without hitting the backspace/delete key:
GET / HTTP/1.0
Then hit the Enter key twice.
The first "/" denotes the root page of the server. Note that there are spaces before and after the first /, but none in "HTTP/1.0". The server should send back a response to your request, consisting of the HTML file. Since the telnet program is not a web browser, it does not understand HTML tags and formatting so it just displays the HTML back to you as plain text.
The GET command is one of the commands of HTTP. You may find details of others by doing a Google search for "HTTP protocol". For example, the following pages may be helpful if you are looking for a little bit more information: http://en.wikipedia.org/wiki/Http and http://www.jmarshall.com/easy/http/.
Connecting to a POP server
One common protocol used by mail servers is POP (Post Office Protocol). You can also use telnet to interact with a POP server, for example, mail.berry.edu. You first need to find out the default port used by the POP protocol. Do a Google search to find this and then connect to mail.berry.edu on that port.
Caution: The following steps will require you to type your password visibly in the SSH terminal. If you do not feel comfortable doing so (e.g. someone is sitting close to you), you may skip this section.
The following are commands you must use with the POP server:
USER userid
This must be the first command after the connect. Supply your e-mail userid (not the full e-mail address). Example: USER john.smith
PASS password
This must be the next command after USER. Supply your e-mail password. The password may be case sensitive.
The following commands may be used as needed:
STAT
The response to this is: +OK #msgs #bytes Where #msgs is the number of messages in the mail box and #bytes is the total bytes used by all messages. Sample response: +OK 3 345910
LIST
The response to this lists a line for each message with its number and size in bytes, ending with a period on a line by itself. Sample response:
+OK 3 messages 1 1205 2 305 3 344400 .
RETR msg#
This sends message number msg# to you (displays on the Telnet screen). You probably don't want to do this in Telnet (unless you have turned on Telnet logging). Example: RETR 2
TOP msg# #lines
This is an optional POP3 command. Not all POP3 servers support it. It lists the header for msg# and the first #lines of the message text. For example, TOP 1 0 would list just the headers for message 1, where as TOP 1 5 would list the headers and first 5 lines of the message text.
DELE msg#
This marks message number msg# for deletion from the server. This is the way to get rid a problem causing message. It is not actually deleted until the QUIT command is issued. If you lose the connection to the mail server before issuing the QUIT command, the server should not delete any messages. Example: DELE 3
RSET
This resets (unmarks) any messages previously marked for deletion in this session so that the QUIT command will not delete them.
QUIT
This deletes any messages marked for deletion, and then logs you off of the mail server. This is the last command to use. This does not disconnect you from the ISP, just the mailbox.
Again, there are other POP3 commands but these are the primary ones. Mail clients, such as Outlook or Eudora, will interact with a POP mail server using these commands and provide you with a fancier GUI.
Java Socket Programming Example
Read Section 24.3 (page 913-914) and then create a new project called "network" in your Eclipse workspace. Download the following program into the project: lab11/WebGet.java and run it.
Retrieving Time from a Time Server
Write a program that uses a socket to retrieve the correct time from a timeserver using the DAYTIME protocol.
There are many servers on the Internet that provide a time service. The National Institute of Standards and Technology http://www.bldrdoc.gov/ maintains several timeservers that allow users to synchronize a clock. One protocol used is the Daytime protocol. The Daytime protocol is a simple protocol. The user connects on port 13 and sends a newline character. The server responds by sending back an ASCII string and then disconnects. The time string from the NIST servers is in the following format:
JJJJJ YR-MO-DA HH:MM:SS TT L H msADV UTC(NIST) *
In this simple exercise, we are concerned only with the first half of the string.
JJJJJ represents the last five digits of the Julian date. You can ignore this value.
YR-MO-DA YR represents the last two digits of the year, MO represents the month and DA, the current day.
HH:MM:SS is the time in hours (HH), minutes (MM) and seconds (SS). This is sent in Coordinated Universal time.
You need to add an offset to correct the time to your time zone.
Write a program that connects to three of the NIST servers listed below, using the Daytime protocol. Parse the return string and create a Date object set to the return string's time. Print out the return strings and your Date objects. Use a SimpleDateFormat to format your date. Make sure you adjust the time to your correct time zone. You should read the Java API documentation to obtain more information on the SimpleDateFormat class. Basically you will need to use the class to parse the string (or substring of) returned by the server. For example, the following formatter is able to format a string of the form "05-02-25 17:30:49 UTP", by using its parse method:
SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss z");
After obtaining a Date object using formatter.parse, you can format it to your local time using the format method of the DateFormat class:
DateFormat localFormatter = DateFormat.getDateTimeInstance();
NIST Timeservers
Name |
Location |
time-a.nist.gov time-b.nist.gov time-a.timefreq.bldrdoc.gov time-b.timefreq.bldrdoc.gov time-c.timefreq.bldrdoc.gov utcnist.colorado.edu time.nist.gov time-nw.nist.gov nist1.datum.com nist1.dc.glassey.com nist1.ny.glassey.com nist1.sj.glassey.com nist1.aol-ca.truetime.com nist1.aol-va.truetime.com |
NIST, Gaithersburg, Maryland NIST, Gaithersburg, Maryland NIST, Boulder, Colorado NIST, Boulder, Colorado NIST, Boulder, Colorado University of Colorado, Boulder NCAR, Boulder, Colorado Microsoft, Redmond, Washington Datum, San Jose, California Abovenet, Virginia Abovenet, New York City Abovenet, San Jose, California TrueTime, AOL facility, Sunnyvale, California TrueTime, AOL facility, Virginia |
Show me your program when done.
A Server Program
Download, study, and run the following code, accompanying Section 24.4 in the textbook (pages 916 - 918).
- lab11/BankAccount.java
- lab11/BankServer.java
- lab11/BankService.java
- lab11/Bank.java
- lab11/BankClient.java
Homework Exercises
I have posted the points breakdown of how your projects will be graded. Take a look at it on the Group project page.
[80 points] Write a simple web server that recognizes only the GET request. When a client connects to your server and sends a command, such as GET filename HTTP/1.0, then return a header
HTTP/1.1 200 OKfollowed by a blank line and all the lines in the file. If the file doesn't exist, return
404 Not Found
instead.
Your server should listen to port 8080. Test your web server by
starting up your web browser and loading a page, such as localhost:8080/c:/cs1/myfile.html
.