Internet and Intranet Protocols and Applications

 

Programming Project 4 - FTP Client.

Revision 0: March 17, 2002

Revision 1: March 21, 2002 [change due date]

Revision 2: April 1, 2002 [correct example]

Revision 3: April 5, 2002 [correct example again]

Revision 4: April 6, 2002 [change reference to "Host command" to "OPEN command"]

 

Due Date: April 10

 

Description

 

Write a program that that implements a subset of the client-side FTP protocol.

 

You should read the class notes (Lecture 6) and RFC 959 for specific information about the FTP protocol and operation.

 

Your program MUST implement the following FTP commands:

 

 

You must check the response code from the server to each of these commands AND to the initial connection request (FTP Server sends a 220 response plus some text when you connect).  You should read section 4.2 of the RFC very carefully, particularly the section on "multi-line replies"!

 

If you receive any response code that indicates an error, then abort the command and prompt for a new command.  Make usre that you print the response exactly as you received it.

 

 

Program Operation

 

Your program should "prompt the user with the prompt "? " and accept the following user commands from the keyboard:

 

Open <host-name> [<port>]

Specifies the name of the FTP server <host-name> and an optional port number  <port> on that host.  If <port> is not specified, use the default FTP port (21).    When you receive a valid Host command, prompt the user for a user name and a password as follows:

 

User?               [user enters a user ID]

Password?        [user enters a password]

 

Then connect to the FTP server.  This connection is referred to as the control connection and the port is the control port.

 

After you have received a correct reply to the connection, log on to the FTP server by sending the USER and PASS commands, specifying the user name and password given by the user.  Make sure that you send the USER command and check the server's reply BEFORE you send the PASS command.  Don't forget to check for an appropriate response to the PASS command!

 

This command must be the first command entered.  If any other command is entered BEFORE you have connected to the server, reject the command with the error message:

 

"No Host!", then continue with another prompt.

 

Once you are logged into a server, this command should be prohibited.  If the Host command is entered while you are already logged into a server, reject the command with the message:

 

"Already Connected to server HHH" where HHH is the host to which you are connected.

 

 

Asc

          Sets the data transfer TYPE to ASCII.  ASCII is the default mode.

 

Bin

          Sets the data transfer TYPE to IMAGE.

 

Put  <local_filename> [<remote_filename>]

Send a file from the local system to the remote system.   If the optional <remote_filename> is specfied, then provide this in the STOR command, else use <local_filename> in the STOR command.  <local_filename> is the name of the file on the local system to send to the server.

 

Get  <remote_filename> [<local_filename>]

Get a file named <remote_filename> from the remote system to the local system.  Provide  <remote_filename> in the RETR command.  If the optional  <local_filename> is specified, then write the received file to <local_filename> on the local system; else write the received file to <remote_filename> on the local system.

 

Bye

Send the QUIT command (and wait for a response!), then close the any sockets and open files.  You are now disconnected from the FTP server and are now permitted to accept another OPEN command.

 

 

Quit

Close any sockets (and any open files) and exit.  This is how your program should end normally.

 

 

NOTE: Your program should accept any case for (upper/lower) for these commands.

 

 

 

Program Output

 

You MUST write each command that you send to the server, prefixed by "---> ", to standard out.

You MUST write each response that you get from the server to standard out EXACTLY as you receive it, INCLUDING the initial response that the server sends when you connect to the server on the control port.

 

See example below.

 

Establishing the Data Connection.

 

The server will operate in "Active" mode.  This means that the server will initiate the data connection when you send a RETR or STOR command, and your program MUST be ready to accept a socket connection on the designated port.  You designate the port to the server by sending the PORT command to the server BEFORE you send the RETR or STOR command.  This port number is called the "data port" and your program is referred to as the "passive data transfer process" in RFC 959.

 

You MUST read section 3.2 AND 3.3 of RFC 959 carefully.  In this assignment, you are required to allocate a new Server socket to use for the data connection for EACH file transfer.  Use the port number assigned to the socket by the system for the data connection by sending the port number for this socket to the server using the PORT command.  Make sure that you are listening on this socket BEFORE you send the STOR or RETR commands!

 

 

File Transmission Mode.

 

The default transmission mode for FTP is STREAM mode (see section 3.4.1 of the RFC).  This is the mode that you are required to use.  You need not provide support for any other transmission mode.  Since STREAM mode requires that the sending side close the data connection socket to signal end of file, your program MUST be prepared to allocate a new socket for EACH file.

 

 

How to Test Your Program.

 

The Solaris machines to which you have access (by now, EVERYONE should have an account on the CS machines) all run an FTP server.  You have FTP access to your home directories on the CS machines.  Use these accounts to test with.  Make sure that you can send and receive binary and ASCII files.  Make sure that all of the commands - both user interface and FTP commands - work properly.  We will certainly ask your program to put or get a file that does not exist so make sure that you handle this error too (if you check responses codes properly, then you will have no problem!).

 

Hand In:

 

1.      Source code (all of your java files).

2.      A text file describing how to compile your program and how to run the program

 

Place these files into a jar file and email the jar file to your assigned TA.

Name your jar file  <sid>.jar where <sid> is your student ID.

REMEMBER to put your name and student ID in the subject line of your email as follows:

 

Project 4 <name>:<sid>

 

 

 

Example of Program Operation:

 

Suppose my main program is in a class named FTPClient:

 

Java  FTPClient  (note, CLASSPATH may need to be specified: it's omitted in this example)

 

? open dept.cs.nyu.edu

 

user?  bbunny

password?  Whatsupdoc

 

{now you connect to dept.cs.nyu.edu on port 21}

 

220-

220-***************************************************************************

*

220-*

*

220-*               Courant Institute Workstation Network

*

220-*

*

220-*  All problems/questions should be addressed to comment@cims.

*

220-*

*

220-***************************************************************************

*

220-

220 dept FTP server (SunOS 5.8) ready.

 

---> USER bbunny

331 Password required for bbunny

 

---> PASS whatsupdoc

230 User bbunny logged in.

 

? bin

 

---> TYPE I

200 Type set to I.

 

? put testMe.java

 

---> PORT 141,157,210,43,6,18

200 PORT command successful.

---> STOR testMe.java

150 Binary data connection for testMe.java (141.157.210.43,1554).

226 Transfer complete.

 

? put testMe.java  testMe2.java

 

---> PORT 141,157,210,43,6,19

200 PORT command successful.

---> STOR testMe2.java

150 Binary data connection for testMe2.java (141.157.210.43,1555).

226 Transfer complete.

 

? get testMe2.java

 

---> PORT 141,157,210,43,6,20

200 PORT command successful.

---> RETR testMe2.java

150 Binary data connection for testMe2.java (141.157.210.43,1556) (408 bytes).

226 Binary Transfer complete.

 

? asc

 

---> TYPE A

200 Type set to A.

 

? get testMe2  testMe3.java

 

---> PORT 141,157,210,43,6,21

200 PORT command successful.

---> RETR testMe2

550 testMe2: No such file or directory.

 

? get testMe2.java  testMe3.java

 

---> PORT 141,157,210,43,6,22

200 PORT command successful.

---> RETR testMe.java

150 ASCII data connection for testMe.java (141.157.210.43,1558) (408 bytes).

226 ASCII Transfer complete.

 

? bye

 

---> QUIT

221 Goodbye.

 

{At this point, you should have no open sockets and you SHOULD be able to accept another "OPEN" command, allowing a new FTP session with a new host!}

 

? quit

 

{You should close any open sockets and exit your program}

 

 

 

WARNING ABOUT COPYING AND PLAGIARISM:

We use tools that compare program source code and detect similarly constructed programs (and classes).  So, if you copy code from a classmate we will detect it, even if you change ALL of the variable names AND move the code around in the file!  So, PLEASE do your own work.  Remember that the penalty for copying is NO CREDIT for the assignment.