Transfer of Data Between Client and Server
Design DocumentThe idea of the project is to allow data transfer between server and client. A client can either upload a file or download a file from or to the server. Upload means data transfer is done from the client going to server and download means data transfer is done from server to client. The protocol being used is UDP in which data are being sent in packets. It means that when we want to transfer a file, we have to break it down into packets and send them. It is not possible to send one big file as is to the network. UDP is a connectionless protocol. It means that there is no guarantee that all packets will be received by the receiver. In other words, file transfer can be faulty and unreliable. When packets get lost, upload/download of the file is incomplete and the ending result is a corrupted file.
Server ClassThe server class opens a port where to listen for incoming packets. When a packet arrives, it will be rebuilt as a “Message” object. The object contains details about the received data that came from a client. If the received message is for upload, the server will simply extract the file data and write it to a file. If the received message is for download, the server will check how far the client has already downloaded a file and then gets the next chunk of data and sends it back to the client.
Client ClassThe client class gives the option to the user whether to upload or download a file. If the user wants to download a file, they have to enter a filename of a file that exists on the server. The client will keep track of how much data has already been collected. It will keep on sending the server these data as a "Message" object. The server will receive it and then knows the next chunk of data to give back to the client. The process repeats until the client receives all data that is needed and writes them to file. If the user wants to upload a file, the program will break down the file into chunks and then puts each chunk as a “Message” object. For every message, it sends it to the server to receive. The server will collect and get all these chunks of data and write it to a file.
Message ClassAn object that contains details of a packet being transferred between client and server. These details include:
- Filename: Name of a file being uploaded or downloaded.
- File data: Partial bytes of data of a file that is currently being uploaded or downloaded.
- File data size: The size of the bytes of data being uploaded or downloaded.
- Expected File Size: This is only useful when performing a download. This information will let the downloader know how much data needs to be transferred.
- Downloaded File Size: This is only used when performing a download. This information will let the server know how much data has already been downloaded by the client.
- Type: Defines the type of message whether the data it contains is for “Upload” or “Download”.
Sample Scenario ExecutionHere we have the server directory which contains the classes to run a server and the directory called “server_files” where uploaded files or files to download goes.
This would be the initial content of the “server_files”:
Here we have the server directory which contains the classes to run a server and the directory called “server_files” where uploaded files or files to download goes.
This would be the initial content of the “client_files”:
We open a terminal and run the server for a specific port:
We open another terminal and run the client. The client resides on the same computer as the server that’s why we use the IP address 127.0.0.1. The port should be the same port used by the server when it was executed:
To demonstrate an upload, we choose the upload option and type in the filename of a file that resides in the "client_files” folder:
After a successful upload, the server will receive the file inside the “server_files” folder:
To demonstrate download, we choose the download option and type in the filename of a file that resides in the “server_files” folder of the server:
After a successful download, the downloaded file is inside the "client_files” folder: