Question

I am currently transferring a file from a c++ server using the following c# code

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;

namespace WebRequestTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Making small binary request: 1MB");
            makeRequest("http://server:8081/sm_binary", "sm_copied");

            Console.WriteLine("Making medium binary request: 10MB");
            makeRequest("http://server:8081/md_binary", "md_copied");

            Console.WriteLine("Making large binary request: 100MB");
            makeRequest("http://server:8081/lg_binary", "lg_copied");

            Console.WriteLine("Making huge binary request: 2GB");
            makeRequest("http://server:8081/hg_binary", "hg_copied");


            while (true) { }
        }

        static void makeRequest(string url, string filename)
        {
            Console.WriteLine("Starting request: " + DateTime.Now.ToString("HH-mm-ss-fff"));
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream data = response.GetResponseStream();

            Console.WriteLine("Starting file write: " + DateTime.Now.ToString("HH-mm-ss-fff"));
            using (System.IO.FileStream fs = System.IO.File.Create("./Binaries/" + filename))
            {

                byte[] buffer = new byte[8 * 1024];
                int len;
                while ((len = data.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fs.Write(buffer, 0, len);
                }

                fs.Close();
            }

            Console.WriteLine("Process is done: " + DateTime.Now.ToString("HH-mm-ss-fff") + "\n");
        }
    }
}

I am getting reasonable times on the first three file transfers, with the 100MB transfer and write taking about 43 seconds. The 2GB file transfer is taking obnoxiously long at about 37 minutes compared to the expected ~15 minutes. I would like to make sure that the receiving side of this code is not causing the slowdown and I am wondering if there is a more efficient way to write these files to disk.

Was it helpful?

Solution

You may try using the WebClient class instead to obtain the raw file rather than a streamed block:

WebClient client = new WebClient();
client.DownloadFile(fileUri, filePathOnHardDrive);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top