Click here to Skip to main content
15,893,401 members

multiconnection downloader in C#

mohsenadc asked:

Open original thread
hi i work on a project for download a large file in multiconnection.i used this code but have some problems and didn't work connections together.

program.cs:
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

           

            List<FileDownloader> filewonloadersList = new List<FileDownloader>();
       System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://filetests.hosthop.com/Large_File_Downloads/100M.zip");
            req.Method = "HEAD";
            System.Net.WebResponse resp = req.GetResponse();
            int responseLength = int.Parse(resp.Headers.Get("Content-Length"));
            int downloadparts = responseLength / 5;
            for (int i = 0; i < responseLength; i = i + downloadparts)
            {
                FileDownloader filedownloadlistitem = new FileDownloader();
                filedownloadlistitem.Start = i;
                filedownloadlistitem.End = i + downloadparts;
                filedownloadlistitem.partnumber = filewonloadersList.Count + 1 ;
                filedownloadlistitem.Url = req.RequestUri.ToString();
                filewonloadersList.Add(filedownloadlistitem);
            }

                List<Thread> threadList = new List<Thread>();
                for (int i = filewonloadersList.Count-1; i >= 0; i--)
                {
                    ThreadStart ts = new ThreadStart(filewonloadersList[i].DoDownload);
                    
                    Thread run = new Thread(ts);
                    threadList.Add(run);
                    run.Start();
                ,,}


                //foreach (Thread aThread in threadList)
                //{
                //  //will wait 
        }
    }
}


and Filedownloader.cs is:

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;

namespace ConsoleApplication1
{
        public class FileDownloader{
            public int Start;
            public int End;
            public string Url;
            public object partnumber;
           public  void DoDownload(){

                // Create a New 'HttpWebRequest' object .
               HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
                myHttpWebRequest.AddRange(Start, End);
                //Console.WriteLine("Call AddRange("+Start+","+End+")");
                //Console.Write("Resulting Request Headers: ");
                //Console.WriteLine(myHttpWebRequest.Headers.ToString());

                // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
                HttpWebResponse myHttpWebResponse=null;
                while (myHttpWebResponse == null)
                {
                    try
                    {
                        myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                    }
                    catch { }
                }

                // Displays the headers in the response received
                //Console.Write("Resulting Response Headers: ");
               // Console.WriteLine(myHttpWebResponse.Headers.ToString());

                // Display the contents of the page to the console.
                Stream streamResponse = myHttpWebResponse.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);
                Char[] readBuffer = new Char[256];
                int count = streamRead.Read(readBuffer, 0, 256);
               // Console.WriteLine("\nThe HTML contents of the page from "+Start+" to "+End+" characters are :\n  ");
                TextWriter write2file = new StreamWriter("c:\\testdownloadfile" + Convert.ToString(partnumber) + ".txt", true); 
               
                    while (count > 0)
                {
                    String outputData = new String(readBuffer, 0, count);
                    Console.WriteLine(outputData);
                    
                    write2file.Write(outputData);
                    
                    count = streamRead.Read(readBuffer, 0, 256);
                    Thread.Sleep(1);

                }
                // Release the response object resources.
                streamRead.Close();
                write2file.Close();            
                streamResponse.Close();
                myHttpWebResponse.Close();
                    
            }
        }
}


how can i solve it?plz help!ik
Tags: C#, .NET, Threads

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900