Simple FTP File Transfer using UDP

We are all aware of how we send and receive files over email. But do we know the process behind the simple send and receiving of a file? Well, this post is kind of touching upon on the same topic. This is my learning and experiment in implementing UDP for transferring files i.e. a simple FTP file transfer. For those who doesn’t know how UDP works, please check out this Wikipedia page before moving with the post. The similar concept can be implemented using TCP but utilizing UDP will result in better understanding of the encapsulation, sending and checksum of the packets. In this post, I will try to use Go-Back-N protocol for sending the packets from client side to the server side.

Introduction and Mechanism of the project:

For this post, we will be developing Client and Server side applications to do simple FTP transfer using UDP. First, Client would be establishing the connection with the server. On connection, the client will try to send a file to the server using GoBackN protocol. Here, we are experimenting the code on LocalHost, so there would not be any packet loss while sending the data. To duplicate the working of GoBackN, we are trying to imitate packet loss using random probability. The mechanism for this system is shown below in the image:

simple ftp file transfer

As per the above mechanism, the Client side code of the application which is as follows:

Code Explanation:
In the code above, divideFile method in class Client is to divide the file into smaller chunks of packets whose size is not more than the MSS specified in the command line argument. In this method, we are calling Checksum method to calculate the checksum for the packet and adding that to the encapsulated packet. This checksum method is similar for both Client and Server. The client always sends the complete window and wait for the acknowledgment. As soon as an acknowledgment is received for the first packet sent, the window is slid to right. This similar fashion would be adopted by the Client for all the batch of packets sent to the server.

The Server code is based on receiving the file from the Client where it is waiting for the packet till the last packet arrives. Server tends to reject the packet on following three points:

  • If the Sequence number which arrives was expected by the Server, if not then it is rejected by Server.
  • If the Checksum of the packet received is correct, if not then it is rejected by Server.
  • If randomly found number is greater than the assigned probability loss of packet, if not then it is rejected by Server,

The server code is as follows:

 

Code explanation:

Server class constructor initializes with the basic details such as hostname, port number on which server is listening, filename in which data will be stored, p: probability of losing the packet. startListener is the method which listens to any incoming connection and checks packet for rejecting based on the three points discussed above.

 

Additional Code:

As both the codes are using a particular packet format which had packet field, checksum field, packet data, and end of file field. We encapsulated all this information into a class called Packet. Similarly, acknowledgments are sent from the server which is also encapsulated in the Acknowledgement class. The code for both the class is as follows:

You can get the code from this GitHub link and test it by yourself: https://github.com/intellectape/Simple-ftp
Let me know in case you need help with any query related to socket programming. I would be more than happy to help.
Till then, MAY THE FORCE BE WITH YOU!