Reputation: 6686
Imagine that Byte[] refArchiveData
is around 30-100 mbytes of size. I want to send it to server using NetworkStream stream
object:
TcpClient client = new TcpClient(server, port);
Byte[] refLengthBytes = new Byte[4];
Byte[] refArchiveData = null;
refArchiveData = File.ReadAllBytes("C:/Temp/Python25.zip");
Console.WriteLine("Archive data length is {0}", refArchiveData.Length);
NetworkStream stream = client.GetStream();
refLengthBytes = System.BitConverter.GetBytes(refArchiveData.Length);
stream.Write(refLengthBytes, 0, 4);
stream.Write(refArchiveData, 0, refArchiveData.Length);
stream.Close();
client.Close();
How i must modify the part of code where i send my archive? Maybe split it by chunks of 256-512 bytes?
Thank you for any advice!
Upvotes: 4
Views: 180
Reputation: 10940
Use the Stream.CopyTo method. This way you don't have to load the whole file into an array (into memory).
using (var file = File.OpenRead("<path to file>"))
using (var client = new TcpClient("<server>", <port>))
using (var netStream = client.GetStream())
{
...
file.CopyTo(netStream);
...
}
Upvotes: 7