[C#] Updater

Joined
May 18, 2007
Messages
1,787
Reaction score
291
I've been working on a launcher with an auto updater at start up. Everything works fine all but when it gets like 500kb into downloading the file is throws an exception.


PHP:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Ini;

namespace SALauncher
{
    public partial class Update : Form
    {
        private string ServerIP = "null";
        private string Exec = "null";
        private string InstDir = "null";
        private string PatchFile = "null";


        public Update()
        {
            IniFile ini = new IniFile("./Config.ini");
            ServerIP = ini.IniReadValue("ServerSet", "ServerIP");
            PatchFile = ini.IniReadValue("ServerSet", "PatchFile");
            Exec = ini.IniReadValue("ClientSet", "Exec");
            InstDir = ini.IniReadValue("ClientSet", "InstDir");
            InitializeComponent();
        }

        private void Update_Load(object sender, EventArgs e)
        {
            Ok.Visible = false;
            DLProgress.Text = "Checking for Updates";
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

            string sUrlToReadFileFrom = "http://" + ServerIP + "/"+PatchFile+"";
            string sFilePathToWriteFileTo = "" + InstDir + ""+PatchFile+"";
            Uri url = new Uri(sUrlToReadFileFrom);
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            response.Close();
            Int64 iSize = response.ContentLength;
            Int64 iRunningByteTotal = 0;
            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                using (System.IO.Stream streamRemote = client.OpenRead(new Uri(sUrlToReadFileFrom)))
                {
                    using (Stream streamLocal = new FileStream(sFilePathToWriteFileTo, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        int iByteSize = 0;
                        byte[] byteBuffer = new byte[iSize];
                        while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0) /<------------------ This line gives me the error below.
                        {
                            streamLocal.Write(byteBuffer, 0, iByteSize);
                            iRunningByteTotal += iByteSize;
                            double dIndex = (double)(iRunningByteTotal);
                            double dTotal = (double)byteBuffer.Length;
                            double dProgressPercentage = (dIndex / dTotal);
                            int iProgressPercentage = (int)(dProgressPercentage * 100);
                            backgroundWorker1.ReportProgress(iProgressPercentage);
                        }
                        streamLocal.Close();
                    }
                    streamRemote.Close();
                }
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            DLProgress.Text = "Update In Progress";
            UpdateBar.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Ok.Visible = true;
            this.Close();
        }

        private void Ok_Click(object sender, EventArgs e)
        {
            Form Update = new Form();
            Update.Close();
        }
    }
}
The line
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
Gives me this error.
Code:
Unable to read data from the transport connection: An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.

It doesn't seem to do it with smaller files only larger ones. Is it a limit that's unfixable? or how do I go about fixing it.
 
replace

PHP:
int iByteSize = 0;

with this

PHP:
 int[] iByteSize;
iByteSize  = new int[10]

my theory behind that is that each bytesize can be represented as an element in the array
so you can really change the array size to any that fits the task it'll need to perform. Or
you can use an dynamically allocated array.
 
Last edited:
Back