Noufy Noufal
Noufy Noufal

Reputation: 1

Peer to peer file transfer in java

I am trying to make a peer to peer file sharing network in Java. I am using UDP connection to send packets over the network. But I can't transfer a complete file though a UDP connection. Most of the packets are missing every time. If I send the packets with delay the receiver get all the packets.

This is my code for sender:

while (fso.hasNextBlock()){   
   byte[] temp1 =fso.nextBlock();   
   int size1 = temp1.length;  

   packet=new DatagramPacket(temp1, size1,ipaddress,port);
   socket.send(packet);                           

   // Thread.sleep(100);
} 

This is the receiver code:

FileOutputStream  out=new FileOutputStream(file);
byte[] data=new byte[size];
DatagramPacket packet=new DatagramPacket(data, data.length);
int i=0;
while(true){                   
    socket.receive(packet);
    out.write(packet.getData());
    System.out.println("packet "+i);
    i++;
    ..... // some codes                    
}

Is anything wrong in my code?
I am not familiar with peer to peer networks. Which is better to use, TCP or UDP in file sharing networks?

Upvotes: 0

Views: 3095

Answers (2)

Seth Noble
Seth Noble

Reputation: 3303

What you are seeing is packet loss due to sending the data faster than the network path can handle it. UDP has no flow control or loss recovery. If you just dump packets into the network, router queues will overflow and drop some of your packets. That's why slowing it down reduces the losses.

To use UDP for file transfer, you need to implement your own flow control and loss recovery algorithms. That is not easy to do, and quite difficult to do well. UDP based protocols are typically used where TCP algorithms break down, simply because UDP provides a clean slate to implement new algorithms.

Unless you want to get deep into network transport design theory, you are better off either using TCP or looking at one of the many UDP file transfer protocols that already exist.

If you are curious to design your own, I suggest that you start with a background on network performance statistics. I have written an article on this topic: Loss, Latency, and Speed. The article is focused on network performance analysis, but it also talks about why networks behave the way they do, which is essential for understanding flow control.

A list of existing UDP protocols can be found on Wikipedia. But unless you have some compelling performance reason (or compelling curiosity), you are better off sticking with TCP.

Disclaimer: I work for the company which produces Multipurpose Transaction Protocol.

Upvotes: 2

Philipp
Philipp

Reputation: 69663

The UDP protocol is unreliable by design. It doesn't guarantee that packets are received in-order and received at all.

When you want to implement a file transfer protocol via UDP, you need to implement this yourself.

  1. prefix every packet with a sequence number
  2. check the sequence number on the receiver to avoid reassembling them in the wrong order
  3. when the receiver misses a sequence number which isn't received in a reasonable timeframe, implement some mechanism for the receiver to ask the sender to retransmit it.

Or just use TCP which does exactly that for you behind the scenes.

When you don't have a good reason to use UDP for a file transfer, then use TCP because then you don't have to worry about all that. There is a reason HTTP, FTP and BitTorrent, the three file transfer protocols which are used most widely on the internet, use TCP.

Upvotes: 4

Related Questions