File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Java Reliable UDP file transfer implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Java Reliable UDP file transfer implementation" Watch "Java Reliable UDP file transfer implementation" New topic

Java Reliable UDP file transfer implementation

Tony Stark

Joined: Mar 02, 2014
Posts: 2
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

Joined: Sep 20, 2010
Posts: 4629

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?

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Henry Wong

Joined: Sep 28, 2004
Posts: 20535

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Java Reliable UDP file transfer implementation
It's not a secret anymore!