Advertisement

Small File Transfer Question

Started by July 23, 2002 04:52 PM
1 comment, last by snowball123 22 years, 6 months ago
Hi, I created a small app to transfer a single file. -Uses TCP, Opens only one socket -opens a file -Sends the Packet to other node that i''m sending file (have name of file, etc.) -read data from file, send packet with data from read, repeat until all data read -when done, send Packet to other node to clarify i''m finished Is this inefficient? Should I read all data and store it into memory before sending? Typically, there is some latency(delay) when reading a file and if i''m reading and sending alot, I think it would be slow but I''m not too sure. Also, how much data should I send in each packet? Thanks
It wouldn''t hurt to read in the entire size of the file with a read() call, then send pieces over the socket. What you have seems like it will work fine, but yeah, it is a wee bit less efficient, depending on the file size.
Advertisement
actually it would hurt. if you read an entire file to memory it then gets stored in memory which means you allocate the entire file. if its big then this will cause writes to vmem as things are paged out. also if the transfer is long then the file in memory will be paged anyway. sending in 4096 kb chunks is pretty efficent, and there is little chance your TCP link will be faster then your harddrive. you will get TONS of latency reading and allocating an entire file in ram. think about when loading a game, its takes a while you want it to take long for the file transfer to just start? it only become less efficent when the harddrive is slower then the TCP link. which is quite rare.

the way you are doing things is fine. just make sure you send the size of teh file as well in the intial handshake so that the other end knows how large it is and can guess how long it will take depending on transfer rate they are getting.

if you want more data in ram at once, then start with a 16KB buffer and send 4KB at a time. reading from the file in 4KB chunks in another thread.

also that intial delay is going to happen all the time regardless of if you read the entire file into ram. its the harddrive/windows seeking and buffering. after you start the reads and sends you will notice things will move along at a good pace. it wont be faster reading the entire file. though you should use 1KB, 2KB, 4KB or 8KB chunks since thats what the harddrive "likes" (even though windows buffers this away anyway).

i mean web servers dont store entire files in memory, nor do ftp servers, mp3 streaming servers, smb servers, etc. its juts a waste of memory and has TONS of bad side effects which delay the start of transmission. you could for small (ie less then 16KB os so) files buffer them completly in ram at the start if you want, but i doubt you will see an improviment (i never noticed one, and this is on a 100 Mb switched lan where you would notice hardrive/file reading latency the most). in fact reading entire files was overall slower due to the pre buffering done, especially as ram got crowded with other apps running or medium to large files.

This topic is closed to new replies.

Advertisement