Spring 2022 IS496 Programming Assignment 2 - File Transfer Protocol (FTP)


Total Points: 100 points
Goal: Program a Prototype of a FTP application using TCP
Assigned: February 21, 2022
Due: March 7, 2022 March 9, 2022 by the end of day (11:59 pm CST).
Grouping: To be completed by a group.
Note: This instruction is based on C/C++. The Python version is available at here.

Background

In this programming assignment, you will implement the client and server sides of a simple File Transfer Protocol (FTP) application. The file transfer itself will take place using TCP with the client determining the operation to be performed (creating a file, deleting a file, creating a new directory, removing a directory, changing to a different directory, obtaining a directory listing, or closing the connection). The server should respond appropriately to the specific command. The specifics of the protocol are detailed in this document. Note: please refer to appendix A for the port number assigned to you (the same as PG1).

Problem:

Part 1: TCP Practice

In the first part of this assignment, you are asked to build a simple TCP server and client where the client can successfully 1) establish the connection with the server, and 2) send a string (e.g., "Hello World") to the client. The finished code for Part 1 can also be used for Part 2 of this assignment.

We have provided an optional starter skeleton code to get you started on the right track. The starter code files (i.e., tcpserver.c and tcpclient.c) can be downloaded from here.

Part 2: Simple File Transfer Protocol

  1. Server opens the port, and goes into "wait for connection" state.
  2. Client connects to the server on the appropriate port.
  3. Server goes into "wait for operation from client" state and waits for operation command from the client.
  4. Client goes into "prompt user for operation" state and prompts user for operation.
  5. Client passes operation (DN: Download, UP: Upload, RM: Remove File, LS: List, MKDIR: Make Directory, RMDIR: Remove Directory, CD: Change Directory, QUIT: Quit) to server.
  6. Operation is executed as follows:
    1. DN:
      Note: Please test DN with the provided test file (i.e., TestFile.txt) discussed in the general notes.
      1. Client sends operation (DN) to download a file from the server.
      2. Client sends the length of the filename (short int) followed by the filename (character string).
      3. Server decodes what it receives, and checks to see if the file exists in its local directory.
        1. If the file exists, server returns the size of the file to the client as a 32-bit integer.
        2. If the file does not exist, server will return a negative confirmation (32-bit integer value -1). After that, the server should return to the "wait for operation from client" state.
      4. Client receives the 32-bit file length from server.
        1. Client should decode the 32-bit value.
          1. If the value is -1, user should be informed that the file does not exist on the server. Client should return to "prompt user for operation" state.
          2. If the value is not -1, save the value as the file size for later use.
      5. Server sends the file to client.
        1. Client reads "file size" bytes from server.
        2. The client saves the file to disk as "filename".
        3. Inform user that the transfer was successful, and return to "prompt user for operation" state. The client displays throughput results for the transfer.

    2. UP:
      1. Client sends operation (UP) to upload a local file to a server.
      2. Client sends the length of the filename which will be sent (short int) followed by the filename (character string). Note: you can create a local file called "upload.txt" with random strings to test the UP operation.
      3. Server receives the above information, decodes filename size and filename, and acknowledges that it is ready to receive (it is up to you how to do the acknowledgment).
      4. Client replies with a 32-bit value which is the size of the file (in bytes).
      5. Server receives and decodes the 32-bit value.
        1. Server enters the loop to receive file
        2. Client sends file to server. Server reads "file size" bytes from client and saves them to disk as "filename".
        3. Server computes throughput results for the transfer and sends it to the client.
        4. Server sends the throughput results to the client. The client displays the throughput of the transfer.


    3. RM:
      1. Client sends operation (RM) to delete a file from the server. Note: you can create a test file called "delete.txt" in the server directory to test the RM operation.
      2. Client sends the length of the filename which will be sent (short int) followed by the filename (character string).
      3. Server receives the above information, decodes filename size and filename, and checks if the file to be deleted exists or not.
        1. If the file exists, the server sends a positive confirmation (integer value 1) back to the client.
        2. If the file does not exit, the server sends a negative confirmation (integer value -1) back to the client.
      4. Client receives the confirmation from the server.
        1. If the confirmation is negative, it should inform the user that the file does not exist on the server and return to "prompt user for operation" state.
        2. If the confirmation is positive, the client further confirms if the user wants to delete the file: "Yes" to delete, "No" to ignore. The client then sends the user's confirmation (i.e., "Yes" or "No") back to the server.
          1. If the user's confirmation is "Yes", the client waits for the server to send the confirmation of file deletion.
          2. If the user's confirmation is "No", the client prints out "Delete abandoned by the user!" and returns to "prompt user for operation" state.
      5. The server waits for the delete confirmation sent by the client.
        1. If the confirmation is "Yes", the server deletes the requested file and returns an acknowledgment to the client to indicate the success or failure of file deletion operation.
        2. If the confirmation is "No", the server returns to "wait for operation from client" state.

    4. LS:
      1. Client sends operation (LS) to list the directory at the server.
      2. Server obtains listing of it's directory, including both the permission settings and the name of each file (e.g., "-rwxr-xr-x 1 netid dip 21K Aug 22 12:58 test.txt" or simply "-rwxr-xr-x test.txt" )
      3. Server computes the size of directory listing and sends the size to client as a 32-bit integer.
        1. Client receives the size, and goes into a loop to read directory listing.
        2. Once the listing is received, client displays the listing to user.
        3. Client and server return to "prompt user for operation" and "wait for operation from client" state respectively.

    5. MKDIR:
      1. Client sends operation (MKDIR) to make a directory on the server.
      2. Client sends the length of the directory name which will be sent (short int) followed by the directory name (character string).
      3. Server receives the above information, decodes directory name size and directory name, and checks if the directory to be created exists or not.
        1. If the directory exists, the server sends a negative confirmation (integer value -2) back to the client.
        2. If the directory does not exit, the server sends a positive confirmation (integer value 1) back to the client if the directory is successfully created; otherwise, the server sends a negative confirmation (integer value -1) back to the client.
      4. Client receives the confirmation from the server.
        1. If the confirmation is negative (-2), it prints out "The directory already exists on server" and returns to "prompt user for operation/wait for operation" state.
        2. If the confirmation is negative (-1), it prints out "Error in making directory" and returns to "prompt user for operation/wait for operation" state.
        3. If the confirmation is positive, the client prints out "The directory was successfully made" and returns to "prompt user for operation/wait for operation" state.

    6. RMDIR:
      1. Client sends operation (RMDIR) to remove a directory on the server. Note: Directory must be empty for RMDIR to work.
      2. Client sends the length of the directory name which will be sent (short int) followed by the directory name (character string).
      3. Server receives the above information, decodes directory name size and directory name, and checks if the directory to be deleted exists or not.
        1. If the directory exists and is empty, the server sends a positive confirmation (integer value 1) back to the client.
        2. If the directory does not exit, the server sends a negative confirmation (integer value -1) back to the client.
        3. If the directory exists and is not empty, the server sends a negative confirmation (integer value -2) back to the client.
      4. Client receives the confirmation from the server.
        1. If the confirmation is negative (-1), it prints out "The directory does not exist on server" and returns to "prompt user for operation/wait for operation" state.
        2. If the confirmation is negative (-2), it prints out "The directory is not empty" and returns to "prompt user for operation/wait for operation" state.
        3. If the confirmation is positive, the client further confirms if the user wants to delete the directory: "Yes" to delete, "No" to ignore. The client then sends the user's confirmation (i.e., "Yes" or "No") back to the server.
          1. If the user's confirmation is "Yes" the client waits for the server to send the confirmation of directory deletion.
          2. If the user's confirmation is "No" the client prints out "Delete abandoned by the user!" and returns to prompt user for operation/wait for operation state.
      5. The server waits for the delete confirmation sent by the client.
        1. If the confirmation is "Yes" the server deletes the requested directory and returns an acknowledgment to the client to indicate the success or failure of file deletion operation. (Directory must be empty in order for deletion to occur).
          1. If the acknowledgment is positive, the client prints out "Directory deleted" and returns to "prompt user for operation" state.
          2. If the acknowledgment is negative, the client prints out "Failed to delete directory" and returns to "prompt user for operation" state.
        2. If the confirmation is "No" the server returns to "wait for operation" state.

    7. CD:
      1. Client sends operation (CD) to change to a different directory on the server. Note: You can use CD followed by LS to verify that CD worked successfully.
      2. Client sends the length of the directory name which will be sent (short int) followed by the directory name (character string).
      3. Server receives the above information, decodes directory name size and directory name, and checks if the directory to be changed to exists or not.
        1. If the directory exists, the server sends a positive confirmation (integer value 1) back to the client if it successfully changed directories; otherwise, the server sends a negative confirmation (integer value -1) back to the client.
        2. If the directory does not exist, the server sends a negative confirmation (integer value -2) back to the client.
      4. Client receives the confirmation from the server.
        1. If the confirmation is negative (-2), it prints out "The directory does not exist on server" and returns to "prompt user for operation/wait for operation" state.
        2. If the confirmation is negative (-1), it prints out "Error in changing directory" and returns to "prompt user for operation/wait for operation" state.
        3. If the confirmation is positive, the client prints out "Changed current directory" and returns to "prompt user for operation/wait for operation" state.

    8. QUIT:
      1. Client sends operation (QUIT) to exit.
      2. Client closes the socket, and exits.
      3. Server closes the socket and goes back to the "wait for connection" state.
      4. Client informs user that the session has been closed.

    Note: If it is not explicitly specified, the client and server will return to "prompt user for operation" and "wait for operation from client" state respectively after a successful operation and wait for the next operation.
Important Hint: When uploading and downloading large files, make sure to take into consideration the return value from reading the socket. The system may not provide the same number of bytes as you requested to fill the buffer!

General Notes

FTP-Server Side Design

The server is responsible for handling the connection request from a single client, processing the request, then looping back to handle further requests. For this particular assignment, your code only needs to handle one client at a time. The server binary should be named tcpserver.

The server should listen on the specified port number [the port number assigned to any of your group members] that is given by command line argument. Your server should bind to the port and then listen for incoming client connections. You may decide if you would like to allow timeouts for better responsiveness but any sort of a timeout is purely optional. You may want to allow for port reuse for quicker recovery after a crash.

Once a new client request arrives, your server should use the accept function to create a new client socket. Once the file has been transmitted, return to the state where your server is waiting for a new command. Your server should be invoked as follows:
[netid@is-student00 ~] $ ./tcpserver [Port]

FTP-Client Side Design

The client is responsible for initiating a connection to a server. Once connected, the client code should prompt the user for an operation (DN, UP, HEAD, RM, LS, MKDIR, RMDIR, CD, QUIT), and should transmit the operation to the server. Your client binary should be named tcpclient. Your client should be invoked as follows:
[netid@is-student02 ~] $ ./tcpclient [Server Name] [Port]

The first argument is the hostname of the server to connect to (this will depend on what machine you start your server code on). The second argument is the port number.

Once a transfer completes successfully, you need to display the throughput for the transfer. You should display information similar to the following for each file transfer (DN/UP):

99 bytes transferred in 0.029757 seconds: 0.003326 Megabytes/sec

Demo

Submission

Submit a gzipped tar file of your entire assignment package to the corresponding assignment on Canvas. One submission per group is sufficient. The archive must include the following:

Evaluation Rubric

Your code will be evaluated on one of the student 00/01/02/03 machines based on the following evaluation rubric.

Extra Credits

  1. Download Large Files: The client will be able to download large files (in several MB) from the server. In particular, when the client receives the large file from the server, it will need to enter a loop to receive the entire file in multiple rounds. In each round, the client will be able to write the data up to the buffer size (e.g., 4096).
    Note: You will need take into consideration the return value from reading the socket. The system may not provide the same number of bytes as you requested to fill the buffer. We provided a large files, LargeFile.mp4 (44MB) (download), which will be used to test the DN operation.
  2. MD5 Hash for Data Integrity: You will need to calculate the MD5 hash of the transferred file for data integrity purpose. Whenever you send a file (through UP or DN), calculate the MD5 hash on both the sending and receiving ends.
    • Download (DN): The server will send the calculated MD5 hash to the client. The client will compare the received MD5 hash and the MD5 hash calculated from the received file. If the MD5 hash matches, the client will print a confirmation message with the MD5 hash. Otherwise, the client will display an error message.
    • Upload (UP): The client will send the calculated MD5 hash to the server. The server will compare the received MD5 hash and the MD5 hash calculated from the received file. If the MD5 hash matches, the server will send a confirmation message to the client. Otherwise, the server will send an error message. The client receives the message, and displays it to the user. If the MD5 hash matches, the client will also display the MD5 hash.
    Note: The MD5 hash is to be calculated with the md5sum command line utility. The syntax is md5sum filename to obtain the hash as a string (in hex). You can find more information about MD5 Hash at the Wikipedia Link.
Extra Credit Rubric (20 pts)

Appendix A

Table 1. Port Assignments
Port to UseName
41001 Canty, Angel
41002 Guo, Jerry
41003 Lee, Ivy
41004 Shamim, Iman
41005 Shi, Tiancheng
41006 Shuaibi, Alaa
41007 Torres, Michael
41008 Xiao, Justin
41009 Cui, Cilia
41010 Donelson, Curt
41011 Fang, Hongli
41012 He, Richard
41013 He, Xiang
41014 Huang, Chung-An
41015 Huang, Houshuo
41016 Jia, Bo
41017 Jia, Hao
41018 Jiang, Yuxuan
41019 Li, Chang
41020 Li, Ziheng
41021 Liu, Amelia
41022 Liu, River
41023 Wang, Jingwei
41024 Wang, Weiyu
41025 Wang, Xiuyuan
41026 Wang, Zeyu
41027 Xia, Yilin
41028 Xu, Yuting
41029 Xu, Zhizhou
41030 Yang, Qinwen
41031 Zhang, Boyu
41032 Zhang, Jingfang
41033 Zhang, Yunjia
41034 Zhang, Zheng
41035 Zheng, Shaojun