Using C # to implement multithread file transfer under HTTP protocol

Source: Internet
Author: User
Tags define array bool header net thread tostring trim
Multithreading

Many people have had the experience of downloading Internet files using network ants or network express software, which can greatly speed up the transmission of files on the Internet and reduce the time of file transfer. Why are there so much magic in these software? The main reason is that these software uses the multithread downloading and the breakpoint continues to transmit the technology. If we were to write a program like this on our own, it would be very enjoyable to be able to download files quickly on the Internet. Let me talk about how to use the C # language to write a program that supports multithreaded downloads, and you'll see how easy it is to write a network using the C # language, and also to realize the powerful network features in the C # language.

First, the HTTP protocol, HTTP is the Hpyer Text Transfer protocal abbreviation, it is the most important network protocol on the modern Internet, Hypertext Transfer Protocol in the TCP/IP protocol application layer, is a connectionless, simple, fast C/ S-structured protocols. HTTP's work process is largely divided into four steps of connecting, requesting, responding, and disconnecting. The C # language provides good support for HTTP protocols and provides WebRequest and WebResponse classes in the. NET class library. These two classes are included in the System.Net namespace, and these two classes enable you to implement many advanced network functions, and multithreaded file downloads in this article are implemented using these two classes. WebRequest and WebResponse are abstract base classes that cannot be used directly as objects in a program, and must be inherited, in practice, to select their appropriate subclasses based on the URI prefix in the URI parameter, and for URIs such as HTTP, The HttpWebRequest and HttpWebResponse classes can be used to handle HTTP traffic between the client and the Web server.

The HttpWebRequest class implements a number of advanced features for accessing files on a Web server via HTTP. The HttpWebRequest class provides support for the properties and methods defined in WebRequest, HttpWebRequest exposes the values of public HTTP headers sent to Internet resources as properties, by method or system settings, The commonly used HTTP headers set by a property or method are: accepted, set by the Accept property, connected by the Connection property and KeepAlive property set, Content-length, set by the ContentLength property, Content-type, set by the ContentType property, range, set by the AddRange method. In practice, the header information is correctly set up and delivered to the Web server, and the Web server responds to the request.

The HttpWebResponse class inherits from the WebResponse class and deals specifically with HTTP responses that are returned from the Web server, which implements many methods and has many properties that allow you to fully handle incoming Internet information. In the HttpWebResponse class, for most common HTTP header fields, there are separate attributes that the programmer can use to access the information in the HTTP Receive Message header field. The HttpWebResponse class property used in this example is: ContentLength receives both the length of the content.

With the above understanding, let's look at the use of these two classes, to create HttpWebRequest objects, not directly using the HttpWebRequest constructor, Instead, use the WebRequest.Create method to initialize a HttpWebRequest instance, such as:

HttpWebRequest hwr= (HttpWebRequest) webrequest.create (http://www.163.com/);

After you create this object, you can set the contents of many HTTP header fields, such as HWR, by using the HttpWebRequest property. AddRange (100,1000); Set the received object to a range of 100-1000 bytes.

When the Httpwebreques object uses the GetResponse () method, it returns a HttpWebResponse object that requires the use of HttpWebResponse GetResponseStream () for presenting HTTP return message information. method, which returns a Stream object that can read the message returned by HTTP, such as: First define a Strean object public System.IO.Stream NS; And then Ns=hwr. GetResponse (). GetResponseStream (), you can create a stream object. With the above preparation knowledge, start to design our multithreaded Internet Files download program, first open the Visual Studio.NET integrated development environment, select "File", "New", "project", and then select Visual C # project, in the right list box of the wizard, select Windows application, enter the project name, as in this case: httpftp, and then select the OK button, and the wizard automatically generates a Windows Application project. First, open the Window Designer Design application window and add the following controls:

A list box ListBox1 three text labels label1-label3 three text boxes textbox1-textbox3 a Start Receive button button1 designed window as follows:

The control definition code is:

Public System.Windows.Forms.ListBox ListBox1;
Private System.Windows.Forms.Label Label1;
Private System.Windows.Forms.TextBox TextBox1
Private System.Windows.Forms.Button button1;
Private System.Windows.Forms.Label Label2;
Private System.Windows.Forms.TextBox TextBox2;
Private System.Windows.Forms.Label label3;
Private System.Windows.Forms.TextBox TextBox3;
Private System.Windows.Forms.Label label4;
Private System.Windows.Forms.TextBox textBox4;

Open the Form1 Code Editor and add the following namespaces:

Using system.net;//network Features
Using system.io;//streaming support
using System.Threading;//thread support

Add the following program variables:

Public bool[] THREADW; End of each thread flag
Public string[] filenamew;//each thread receives the file name
Public int[] filestartw;//where each thread receives the start of a file
Public int[] filesizew;//each thread receives the size of the file
public string strurl;//The URL of the accepted file
public bool hb;//File merge flag
public int thread;//Process count

Defines a Httpfile class that manages the receiving thread with the following code:

public class Httpfile
{
Public Form1 FORMM;
public int threadh;//Thread code
public string filename;//file name
public string strurl;//The URL of the receiving file
Public FileStream FS;
public HttpWebRequest request;
Public System.IO.Stream NS;
Public byte[] nbytes;//receive buffer
public int nreadsize;//Bytes Received
Public httpfile (Form1 form,int thread)//construction method
{
Formm=form;
Threadh=thread;
}
~httpfile ()//destructor method
{
FORMM. Dispose ();
}
public void receive ()/Receive thread
{
Filename=formm.filenamew[threadh];
Strurl=formm.strurl;
Ns=null;
nbytes= New byte[512];
nreadsize=0;
Formm.listbox1. Items. ADD ("Thread" +threadh. ToString () + "start receiving");
Fs=new FileStream (filename,system.io.filemode.create);
Try
{
Request= (HttpWebRequest) httpwebrequest.create (strURL);
The starting position of the receive and the length of the received
Request. AddRange (FORMM.FILESTARTW [Threadh],
FORMM.FILESTARTW [Threadh]+formm.filesizew [threadh]);
Ns=request. GetResponse (). GetResponseStream ()//Get receive Stream
Nreadsize=ns. Read (nbytes,0,512);
while (nreadsize>0)
{
Fs. Write (nbytes,0,nreadsize);
Nreadsize=ns. Read (nbytes,0,512);
Formm.listbox1. Items. ADD ("Thread" +threadh. ToString () + "receiving");
}
Fs. Close ();
Ns. Close ();
}
catch (Exception er)
{
MessageBox.Show (Er. message);
Fs. Close ();
}
Formm.listbox1. Items.Add ("Process" +threadh. ToString () + "receive finished!");
Formm.threadw[threadh]=true;
}
}

The class and the Form1 class are in the unified namespace, but are not included in the Form1 class. The following defines the event response function for the Start Receive button control:

private void Button1_Click (object sender, System.EventArgs e)
{
DateTime dt=datetime.now;//Start receiving time
TextBox1.Text =dt. ToString ();
Strurl=textbox2.text. Trim (). ToString ();
HttpWebRequest request;
Long filesize=0;
Try
{
Request= (HttpWebRequest) httpwebrequest.create (strURL);
Filesize=request. GetResponse (). contentlength;//get the length of the target file
Request. Abort ();
}
catch (Exception er)
{
MessageBox.Show (Er. message);
}
Number of received threads
Thread=convert.toint32 (Textbox4.text. Trim (). ToString (), 10);
Initializing an array based on the number of threads
threadw=new bool [thread];
Filenamew=new string [thread];
filestartw=new int [thread];
Filesizew=new Int[thread];

Calculate the size of each thread should receive the file
int filethread= (int) filesize/thread;//average distribution
int filethreade=filethread+ (int) filesize%thread;//the remainder is completed by the last thread
assigning values to arrays
for (int i=0;i<thread;i++)
{
threadw[i]=false;//the initial value of each thread state is false
Filenamew[i]=i.tostring () + ". Dat";//each thread receives the temporary file name of the file
if (i<thread-1)
{
filestartw[i]=filethread*i;//the starting point for each thread to receive the file
filesizew[i]=filethread-1;//each thread receives the length of the file
}
Else
{
Filestartw[i]=filethread*i;
filesizew[i]=filethreade-1;
}
}
Define the thread array, start the receive thread
thread[] threadk=new thread [thread];
httpfile[] httpfile=new httpfile [thread];
for (int j=0;j<thread;j++)
{
Httpfile[j]=new Httpfile (THIS,J);
Threadk[j]=new Thread (new ThreadStart (httpfile[j].receive));
THREADK[J]. Start ();
}
Start merging file threads received by each thread
Thread Hbth=new Thread (new ThreadStart (Hbfile));
Hbth. Start ();
}

The thread hbfile of the merged files is defined in the Form1 class and is defined as follows:

public void Hbfile ()
{
while (true)/wait
{
Hb=true;
    for (int i=0;i<thread;i++)
{
if (threadw[i]==false)//has a thread that is not closed, waiting for
{
Hb=false;
Thread.Sleep (100);
break;  
}
}
if (hb==true)/All threads have ended, stop waiting,
{
break;

}
FileStream fs;//to begin merging
FileStream fstemp;
int readfile;
byte[] bytes=new byte[512];
Fs=new FileS Tream (TextBox3.Text. Trim (). ToString (), System.IO.FileMode.Create);
for (int k=0;k<thread;k++)
{
Fstemp=new FileStream (filenamew[k],system.io.filemode. Open);
while (true)
{
Readfile=fstemp. Read (bytes,0,512);
if (readfile>0)
{
fs. Write (Bytes,0,readfile);   
}
Else
{
break;
}
}
Fstemp. Close ();
}
FS. Close ();
DateTime Dt=datetime.now;
TextBox1.Text =dt. ToString ();//End Time
MessageBox.Show ("Received!!!");

At this point, a multithreaded download file program is done, note that when entering a local file name, you should enter the following format: "C:\\test\\httpftp\\bin\\d.htm", because the "\" character in C # is an escape character, the number of threads is not the greater the better, Generally 5 threads are available, which is passed on the Visual Studio.NET 2002 development environment and the Windows XP operating system.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.