0

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

Foxman
  • 189
  • 13
  • 1
    Don't save the chunks. This is unnecessary as you have discovered. You need to devise a network protocol that allows the other side to reconstruct the file. Probably, you should use TCP. – usr Dec 13 '15 at 13:50
  • _"how does bitTorrent do that"_ - go read the specs. I'm afraid this is a bit too broad to answer. – CodeCaster Dec 13 '15 at 13:52
  • @usr well if the chunks were to arrive in order it would be easy to assemble them one after the other. However, in reality the client will get different chunks from different peers. I can make it more efficient if I don't deconstruct the file but send parts of it with an offset in the data stream, but the receiving end will still have to save the chunks and then rebuild the file. I guess this is possible as it will have to store that data anyway, and then when it rebuilds it it will erase the chunks and if it wants to continue to seed it will operate like the original uploader – Foxman Dec 13 '15 at 14:22
  • 1
    Right, the receiver needs to store them. 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>`. – usr Dec 13 '15 at 14:24
  • @usr oh yeah, I'll initialize a file with zeros but it will have the length of the original one and then I will be able to easily fill the correct areas. Thanks a lot! – Foxman Dec 13 '15 at 16:04

1 Answers1

1

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>>.

usr
  • 168,620
  • 35
  • 240
  • 369