System.OutOfMemoryException on server side for client files

I am getting data from client and saving it to the local drive on local host .I have checked it for a file of 221MB but a test for file of 1Gb gives the following exception:

An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll

Following is the code at server side where exception stems out.

UPDATED

Server:

      public void Thread()
        {
           TcpListener tcpListener = new TcpListener(ipaddr, port);
           tcpListener.Start();
           MessageBox.Show("Listening on port" + port);      
           TcpClient client=new TcpClient();
           int bufferSize = 1024;
           NetworkStream netStream;
           int bytesRead = 0;
           int allBytesRead = 0;

           // Start listening
           tcpListener.Start();

           // Accept client
           client = tcpListener.AcceptTcpClient();
           netStream = client.GetStream();

          // Read length of incoming data to reserver buffer for it
           byte[] length = new byte[4];
           bytesRead = netStream.Read(length, 0, 4);
           int dataLength = BitConverter.ToInt32(length,0);

         // Read the data
           int bytesLeft = dataLength;
           byte[] data = new byte[dataLength];

           while (bytesLeft > 0)
             {

                int nextPacketSize = (bytesLeft > bufferSize) ? bufferSize : bytesLeft;

                bytesRead = netStream.Read(data, allBytesRead, nextPacketSize);
                allBytesRead += bytesRead;
                bytesLeft -= bytesRead;

             }

           // Save  to desktop
           File.WriteAllBytes(@"D:\LALA\Miscellaneous\" + shortFileName, data);

          // Clean up
          netStream.Close();
          client.Close();

    }

I am getting the file size first from client side followed by data.

1).Should i increase the buffer size or any other technique ?

2). File.WriteAllBytes() and File.ReadAllBytes() seems blocking and freezes the PC.Is there any async method for it to help provide the progress of file recieved at server side.

Jon Skeet
people
quotationmark

You don't need to read the whole thing to memory before writing it to disc. Just copy straight from the network stream to a FileStream:

byte[] length = new byte[4];
// TODO: Validate that bytesRead is 4 after this... it's unlikely but *possible*
// that you might not read the whole length in one go.
bytesRead = netStream.Read(length, 0, 4);
int bytesLeft = BitConverter.ToInt32(length,0);

using (var output = File.Create(@"D:\Javed\Miscellaneous\" + shortFileName))
{
    netStream.CopyTo(output, bytesLeft);
}

Note that instead of calling netStream.Close() explicitly, you should use a using statement:

using (Stream netStream = ...)
{
    // Read from it
}

That way the stream will be closed even if an exception is thrown.

people

See more on this question at Stackoverflow