English 中文(简体)
视窗7而不是XP SP2的下载密码作品?
原标题:Download code works in Windows 7 but not XP SP2?

I have wrote a simple piece of code that downloads a file from a url. This code works perfectly in Windows 7, downloading the file at a good speed and maintaining a progress bar showing the download progress. However when I run the same piece of code from Windows XP SP2 it produces a .NET IOException , it makes no sense to me why it doesnt work. It starts the same, completes the first read correctly and then throws the following exception on the second attempt to read from the stream :

 System.IO.IOException: 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. ---> 

 System.Net.Sockets.SocketException: An operation on a socket could not be  
 performed because the system lacked sufficient buffer space or because a queue was full
 at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)

 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
 --- End of inner exception stack trace ---
 at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
 at ServicePackChk.SrvPackChk.DownLoadServicePack(Boolean bWindowsXP)
 at ServicePackChk.SrvPackChk.CheckServicePackStatus()
 at ServicePackChk.Form1.Form1_Load(Object sender, EventArgs e)
 at System.Windows.Forms.Form.OnLoad(EventArgs e)

CODE :

        Uri uri;

        if (bWindowsXP)
            uri = new Uri("http://download.microsoft.com/download/d/3/0/d30e32d8-418a-469d-b600-f32ce3edf42d/WindowsXP-KB936929-SP3-x86-ENU.exe");
        else
            uri = null;

        WebRequest req = WebRequest.Create(uri);
        WebResponse resp = req.GetResponse();

        ProgBarForm pform = new ProgBarForm();

        updateEvent += new SrvPackChk.UpdateDownloaded(pform.UpdateProgBar);

        Stream stream = resp.GetResponseStream();
        ArrayList alBytes = new ArrayList();
        int nLen = (int)resp.ContentLength;


        pform.DownLoadSize = nLen;

        pform.Show();

        byte[] byExe = new byte[nLen];

        bool bMoreToDownload = true;

        FileStream fs = new FileStream(System.IO.Path.GetTempPath() + "XPSP3.exe", FileMode.Create);

        MessageBox.Show("Saving File to " + System.IO.Path.GetTempPath() + "XPSP3.exe");

        while (bMoreToDownload)
        {
            Application.DoEvents();

            int nRead = 0;

            nRead = stream.Read(byExe, 0, nLen);

            nDownloaded += nRead;

            updateEvent(nDownloaded);

            if (nDownloaded == nLen)
            {
                bMoreToDownload = false;
            }

            fs.Write(byExe, 0, nRead);
            fs.Flush();

            Application.DoEvents();
        }

        stream.Close();

        fs.Close();
问题回答

你下载的档案有300多兆字节,你试图从一流读。

最好读一下流体,即4MByte区。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签