Here's my dilema. My client program is going to request a certain file from my server program, so the server program needs to send the size of the file it requests and then the file itself by using a Socket all in one go. My client will then read the size of the data, read the file to a buffer, and write the buffer to a file. Essentially it's just downloading something from the computer my server program is running on.
As the files get larger I'm running in to memory issues. Here's how I send something from the server, with an old attempt at using FileStreams to use less memory. I was using MemoryStreams. Send(File.ReadAllBytes(@"C:\largevideo.avi"));
public void Send(object Data)
{
FileStream SerializeStream = new FileStream(Properties.Settings.Default.TempPath + "serialstrm.TEMP", FileMode.Create); //Create a file to hold serialized data
BinaryFormatter BF = new BinaryFormatter();
BF.Serialize(SerializeStream, Data); //serialize the data to the file
//first 8 bytes = size of data
byte[] Payload = new byte[8 + SerializeStream.Length];
//stupidity right here :P
byte[] ByteData = Data as byte[];
//length of data in byte format
byte[] LengthOfData = BitConverter.GetBytes(ByteData.Length);
//copy length of data to Payload starting at offset 0
Buffer.BlockCopy(LengthOfData, 0, Payload, 0, LengthOfData.Length);
//copy the byte data to Payload starting at offset 8
Buffer.BlockCopy(ByteData, 0, Payload, 8, ByteData.Length);
MasterSocket.Send(Payload);
}
I can't figure out how I can send this to the client without reading the entire file into memory first. I know that using File.ReadAllBytes() is going to put that into the memory. What if the server or client computer doesn't have that much memory to spare? Can I do this without filling up the memory?