Question

I am sending a file over the network using a client-server program. After reading the bytes of the file (with File.ReadAllBytes()), I set the byte array as a field of an object. Then serialize and send the object to the client, where the client should deserialize the object and get the file by using a BinaryWriter.

All the messages between client-Server are passed through serializing an object of a class called Command.

This method seems to consume a lot of memory when the file is loaded to the byte array.

Can anyone propose another mechanism where I can send the file little by little, without consuming too much memory. Is it possible to send the file's memory address and then the server pull the file little by little using the memory address on the client (using a loop)?

Was it helpful?

Solution

I think the best option is to use streaming transfer. It's a native feature in wcf.

You can find help here and here.

EDIT :

You can try to read & send like this way :

  using (FileStream fs = new FileStream(@"C:\...\file.txt", FileMode.Open))
  {
    byte[] buffer = new byte[1024];
    int len;
    while ((len = fs.Read(buffer, 0, buffer.Length)) > 0)
    {
      //client.Send(buffer, 0, len);
    }
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top