Sangeetha
Sangeetha

Reputation: 591

Download using c# code

I am developing c# application, in which i am downloading package(zip file) from server machine.It was downloading properly, but recently our package data has got some changes which is flex application.And by using c# we are downloading it into c drive or d drive.

Now with the new package i am facing some problem as 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.

My code is below

byte[] packageData = null;
packageData = touchServerClient.DownloadFile("/packages/" + this.PackageName);
public byte[] DownloadFile(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remoteSite.Url + url);
            try
            {
                request.Method = "GET";
                request.KeepAlive = false;                

                request.CookieContainer = new CookieContainer();
                if (this.Cookies != null && this.Cookies.Count > 0)
                    request.CookieContainer.Add(this.Cookies);

                HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();                

                // Console.WriteLine(response.StatusDescription);

                Stream responseStream = webResponse.GetResponseStream();

                int contentLength = Convert.ToInt32(webResponse.ContentLength);
                byte[] fileData = StreamToByteArray(responseStream, contentLength);                
                return fileData;
            }



public static byte[] StreamToByteArray(Stream stream, int initialLength)
        {
            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1)
            {
                initialLength = 32768;
            }

            byte[] buffer = new byte[initialLength];
            int read = 0;

            int chunk;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;

                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();

                    // End of stream? If so, we're done
                    if (nextByte == -1)
                    {
                        return buffer;
                    }

                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }

In the above function(StreamToByteArray) , i am getting error as 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.

Please help me on this, coz i am not supposed to change the code also.

Thanks in advance Sangita

Upvotes: 1

Views: 1418

Answers (1)

Paul Walls
Paul Walls

Reputation: 6054

A few things to try:

  • Wrap your stream handling in a using statement.

This will clean up that resource.

using (Stream responseStream = webResponse.GetResponseStream())
{
    int contentLength = Convert.ToInt32(webResponse.ContentLength);
    byte[] fileData = StreamToByteArray(responseStream, contentLength);
    return fileData;        
}
  • Make sure there are no other heavy memory processes running on the same box. Particularly if they are making Socket-bound calls.

  • Try upping the value of the MaxUserPort registry value. Here is the article if you didn't see the link provided in the comments.

Upvotes: 1

Related Questions