Homework 2

Due Monday, October 22.

Goal:

Introduce client/server, TCP/IP programming concepts.

Problem:

Here you will develop an e-mail system. A server program, MailSvr, will run on a given machine, and all mailboxes will be stored on that machine. Users will read and send mail by using a client program, MailClnt, from anywhere on the Internet. Note that MailSvr runs continuously on a single machine, under a single copy, while MailClnt will be run in multiple copies, at various intermittent times, from various locations on the Internet.

Each user has a mailbox, a file on the server machine. The mailbox is password-protected, with the password being stored at the beginning of the file.

Usage format for the server is:

java MailSvr port_number

All users must of course be made aware of the port number. Note that in your context, i.e. a homework context, you must make sure to avoid using port numbers which are already in use on the machine on which you will run your server.

Usage format for the client is (all on one line):

java MailClnt server_address server_port_number cmd user_mailbox password [dest_mailbox][subject]

Here cmd is either r (read mail), s (send mail) or c (create new mailbox); the fields dest_mailbox and subject used only for the s command.

For example, suppose the server is running on our machine pc8, at port 8888, and we have a user in Australia (who, remember, does not need to have an account on pc8). To create a mailbox named Aussie with password LuckyDog, this user would type

java MailClnt pc8.cs.ucdavis.edu 8888 c Aussie LuckyDog

The server would first check to see whether this mailbox name was already taken, and if not, would set up the mailbox and send a confirmation to the user. Later, for this user to read mail, he/she would type

java MailClnt pc8.cs.ucdavis.edu 8888 r Aussie LuckyDog

If this user wished to send mail about lorries to another user, Brit, he/she would type (all on one line)

java MailClnt pc8.cs.ucdavis.edu 8888 s Aussie LuckyDog Brit lorries

When the user specifies the s command, the client will prompt the user to type the body of the message from the keyboard.

The format for a mailbox is

Password:<message><message>...<message>

where<message> has the format

From:<mailbox>Subject:<subject>Body:<body>

Note that a file is, by definition, a sequence of bytes, not Strings. Each message may have multiple lines, but that is up to the user (and you must give the user the opportunity to have multiple lines).

Upon receiving an r command, the server will verify the user mailbox and password. If either is invalid, the server will inform the user of this and close the connection. If both are valid, the server will do the following:

    

       while(true)
       display the Subject: headings of all messages 
          in the mailbox
       ask user which message, say, Message 12, 
          he/she wishes to view
       read response (99 means exit)
       if (99) close socket and break
       else send the message to the client for display

There is no provision for deleting messages.

Don't worry about paging. Assume the user will rely on window scrolling or ctrl-s/ctrl-q flow control if a message or headings list is long.

Upon receiving an s or c command, the server will process the request and then send a confirmation to the client (unless an invalid item was found, in which the server will inform the client), and then close the connection.

Use TCP (not UDP), following my Svr.java and Clnt.java (or the string versions) as models. But do NOT use the Runtime class, i.e. do NOT use commands from the underlying OS; use only Java. For instance, see my CpBytes.java example on how to use Java classes to check whether a file exists.