



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




In this assignment you will extend the program you developed for Homework 1 to parse more FTP
commands and to begin processing file data ⎯ you will also change the syntax of the error and response messages your program prints. In addition to the six FTP commands from HW1, your parser will now recognize the PORT and RETR commands. You will also turn your parser into the basic engine of an FTP server by having it process the RETR command and copying the contents of a file. In addition to handling these two new commands, you will also add more code that will be needed later in your FTP server – for the generation of valid FTP replies and for the enforcement of proper sequences of commands as required by the FTP protocol.
To begin, you will extend your parser to recognize two additional commands:
PORT<SP+><host-port>
In this notation:
The PORT command is used to specify Internet addressing information consisting of a host IP address and port number (We will study the semantics of IP addresses and ports in Chapter 2. For this homework, we will introduce only the conventional syntax for representing them).
For example, the following is a valid PORT command:
PORT 152,2,131,205,31,
The FTP syntax for host IP addresses and port numbers, unfortunately, differs from the conventional syntax and must be translated before it can be used in programs. Specifically, while a valid <host- address> in an FTP command must have commas (“,”) used to separate numbers, the form must be translated to have periods (“.”) as separators before it is used. Thus, the host address 152,2,131, must be translated to 152.2.131.205 for use in programs. The port address is even more idiosyncratic in FTP – the two numbers separated by a comma as in 31,144 are to be interpreted as decimal values contained in the high- and low-order bytes of a 16-bit binary representation of an integer in 2’s complement. It must be converted into the corresponding decimal value. A simple algorithm for doing so is to multiply the value of the leftmost number by 256 and add the value of the rightmost number. For example, 31,144 is converted by the expression (31 * 256) + 144 = 8080.
The RETR command is used to specify the name of a file on the FTP server that is to be copied to the client. For your program, we will assume it specifies the name of a file located in the current working directory from which your program was executed or it specifies the complete pathname for a file located within the space of subdirectories below (rooted in) the current working directory. For example, the following is a valid RETR command:
RETR pictures/jasleen.jpg
It specifies the file jasleen.jpg located in the subdirectory pictures of the working directory used by your program. The syntax of valid RETR commands allows any of the 128 ASCII characters (minus 2) to be specified in the
Once your program has determined that a RETR command is valid, it should “process” the command by copying the named file to a directory named retr_files in the program’s current working directory (be sure to create this directory using the UNIX “mkdir” command before you try to execute your program). The file’s name in the retr_files directory should have the form “filexxx” where xxx is the number of valid RETR commands your program has recognized so far. For example, the first valid RETR command will result in copying the file named in the RETR command to the file named file1 in the retr_files directory; the second valid RETR command will result in a file named file2, etc. The file named in a RETR command may have arbitrary content and should be treated simply as a stream of bytes (Java I/O using the FileInputStream and FileOutputStream classes).
With the addition of PORT and RETR to the six commands from HW1, this completes the minimum subset of FTP commands necessary to have the basic framework for a working FTP server. However, the FTP protocol defines certain constraints on allowable sequences of command/reply exchanges. Briefly stated, they are:
As in Homework 1, your program should read from standard input, echo all input lines and write the corresponding reply to standard output. As before, your program must not output any user prompts, debugging information, status messages, etc.
You should continue processing lines of input until end-of-file is reached on the input stream. If errors are encountered on input lines you should simply emit the appropriate reply and begin the parse of the next command.
Here is an example showing how your output should look with a correct sequence of valid commands (NOTE: in this example the input lines that have been echoed are marked with a “-“ to make the example clear. Do NOT include the “-“ in your program output).
220 COMP 431 FTP server ready. -USER anonymous 331 Guest access OK, send password. -PASS foobar 230 Guest login OK. -SYST 215 UNIX Type: L8. -TYPE I 200 Type set to I. -PORT 152,2,131,205,31, 200 Port command successful (152.2.131.205,8080). -RETR pictures/jasleen.jpg 150 File status okay. 250 Requested file action completed. -QUIT 200 Command OK.
In addition to this output on standard output your program should also copy the content of pictures/jasleen.jpg to retr_files/file1. Here is another example with some error cases:
220 COMP 431 FTP server ready. -USERanonymous 500 Syntax error, command unrecognized. -USER anonymous 331 Guest access OK, send password. -SYST 530 Not logged in. -PASS foobar 230 Guest login OK. -TYPE AI 501 Syntax error in parameter. -PORT 152,2,131,205,31, 200 Port command successful (152.2.131.205,8080). -RETR pictures/smithfd.jpg 150 File status okay. 250 Requested file action completed. -RETR index.html 503 Bad sequence of commands. -QUIT 200 Command OK.
As per the instructions distributed with Homework 1, create a subdirectory named HW2 within the directory ~/comp431/submissions, and place the final version of your program in this directory before the due date. (If you have previously followed the instructions for granting the TAs and instructor access to your comp431 directory, no additional incantations should be required to give us access to your HW directory.) Please give your final program the name “FTPserver2.java.”
When your program is complete, send mail to the TAs. To help further automate the processing of homework give your message the subject line “COMP 431 HW2 ready”. Also, if you send mail from an account other than your CS account, please indicate your Computer Science Unix account login (userid) in the body of the message.
As before, your program(s) should be neatly formatted ( i.e. , easy to read) and well documented. In general, 75% of your grade for a program will be for correctness, 25% for “programming style” (appropriate use of language features, including variable/procedure/class names), and documentation (descriptions of functions, general comments, use of invariants, pre- and post conditions where appropriate).