Search code examples
c#filenetwork-programmingpeerdata-stream

Best way to split files into UDP packet sized chunks for peer to peer file sharing?


I'm working on a P2P file sharing program and in order to pass the files in small bits I need to split the uploaded file somehow. Now, I've made a program that splits a file into small files and puts them in a folder using C# Stream class, and it can also rebuild it. However, it's inefficient and takes a lot of time. I thought of reading the data from the stream with an offset according to the requested file and then sending it without saving. However, I don't know how to add it to the receiving end at the right order as the data will not be sent in order.

On a side note, how does bitTorrent do that kind of functionality?

thanks


Solution

  • The receiver needs to store the chunks. The sender does not. Probably, you should create the entire file on the receiver zero initialized on disk. Then, you can fill in the holes as you receive them. You need a separate structure to track what ranges are there yet, for example a List<Tuple<int, int>>.