Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Reliable UDP file transfer implementation

 
Tony Stark
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone , first post here .....

Anyway, i have a homework for my distributed systems class that requires to implement a reliable UDP file transfer protocol API in Java and create a client server app using that API. I have a good understanding of Java programming and a decent knowledge in networking.
The whole point of the homework is to use ACKs , control flow , sliding window etc.. in the most efficient way and then compare it with TCP and also use different file sizes to transfer.

The problems i face with this project:

- Firstly, from what i understand in order to use the receive method of the DatagramSocket class in the server side you need a DatagramPacket for the received data , witch in turn needs a byte array and the size of the data.
BUT how can i transfer files of different sizes (an mp3 , a pdf , a movie , an iso) if the DatagramPacket object in the server side has a fixed byte array size ...??

- Secondly , If i use numbers or a bit (Alternating Bit Protocol) as a way of packet ordering where would i put that information ?? . I tried to extend DatagramPacket but then saw it was a final class. So should i put that information along with the data of the file and if so
wouldn't that affect the receiving way of the packet ?

- And finally (hopefully), could you suggest a way to ferify the integrity of a packet. A UDP packet has a checksum field but how would i use this in the context of DatagramPacket/Java ..??

Thanks for your time ......
 
Stephan van Hulst
Bartender
Posts: 6337
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not send files completely as one packet. Instead, break them up in pieces that have a set length. That way, your DatagramPackets can use a fixed size, and you receive multiple packets per file.

You put all the information your protocol needs in the data buffer itself. Reserve part of it for your protocol, and part of it is for the file data you're sending.

I'm not sure how Java handles the UDP checksum. At any rate, since you can not access the checksum through DatagramPacket, I suppose that DatagramSocket automatically discards bad packets. If you want to perform your own checks, like a cyclic redundancy check, you should implement it yourself and put the information in your packet's header, much like the information for handshaking, flow control and sliding windows.

Why don't you look at what a TCP/IP packet looks like for inspiration?
 
Henry Wong
author
Marshal
Pie
Posts: 21510
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Stark wrote:
- And finally (hopefully), could you suggest a way to ferify the integrity of a packet. A UDP packet has a checksum field but how would i use this in the context of DatagramPacket/Java ..??


As you know, the UDP protocol is unreliable, but do you know what that means? There is no need to verify the packet, the network (when it delivers the data) actually does a good job with that.

The problem with UDP is ... when the NICs (and their buffers), and the switches in-between, can't deliver your UDP packet, it simply throws it away. Think of it like this ... imagine if the post office finds itself overworked with too much mail to deliver, and decides to fix the problem by throwing half of it in the trash. That is what happens to UDP.

So, how to fix? First, you need to put sequence numbers with each datagram -- meaning when you break your file's data into separate datagrams, you need to know the order of the datagrams, along with the amount of data in each datagram. Second, you need to detect gaps -- meaning on the receiving side, you need to be able to use the sequence numbers to see if you lost any datagrams. And third, you need to add a retransmission capability into your protocol -- meaning once you detect a gap, you need to request the sender to send it again.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic