您的位置:逆风者 VC++ 正文
 添加时间:2007-09-01 原文发表:2007-08-31 人气:81 来源:vckbase.com

本文章共4818字,分3页,当前第2页,快速翻页:
 

  一个列表框 listBox1 三个文本标签 label1-label3 三个文本框 textBox1-textBox3 一个开始接收按钮 button1 设计好的窗口如下图:

逆风编程精品

控件定义代码是:


		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;

      
打开Form1的代码编辑器,增加如下的命名空间:

		using System.Net;//网络功能 

using System.IO;//流支持

using System.Threading ;//线程支持

      
增加如下的程序变量:

		public bool[] threadw; //每个线程结束标志

		public string[] filenamew;//每个线程接收文件的文件名

		public int[] filestartw;//每个线程接收文件的起始位置

		public int[] filesizew;//每个线程接收文件的大小

		public string strurl;//接受文件的URL

		public bool hb;//文件合并标志

		public int thread;//进程数

      
定义一个HttpFile类,用于管理接收线程,其代码如下:

	public class HttpFile

	{

		public Form1 formm;

		public int threadh;//线程代号

		public string filename;//文件名

		public string strUrl;//接收文件的URL

		public FileStream fs;

		public HttpWebRequest request;

		public System.IO.Stream ns;

		public byte[] nbytes;//接收缓冲区

		public int nreadsize;//接收字节数

		public HttpFile(Form1 form,int thread)//构造方法

		{

			formm=form;

			threadh=thread;

		}

		 ~HttpFile()//析构方法

		{

			 formm.Dispose ();

		}

		public void receive()//接收线程

		{

			filename=formm.filenamew[threadh];

			strUrl=formm.strurl;

			ns=null;

			nbytes= new byte[512];

			nreadsize=0;

			formm.listBox1 .Items .Add ("线程" threadh.ToString () "开始接收");

			fs=new FileStream (filename,System.IO.FileMode.Create);

			try

			{

				request=(HttpWebRequest)HttpWebRequest.Create (strUrl);

				//接收的起始位置及接收的长度

				request.AddRange(formm.filestartw [threadh],

						formm.filestartw [threadh] formm.filesizew [threadh]);

				ns=request.GetResponse ().GetResponseStream ();//获得接收流

				nreadsize=ns.Read (nbytes,0,512);

				while (nreadsize>0)

				{

					fs.Write (nbytes,0,nreadsize);

					nreadsize=ns.Read (nbytes,0,512);

					formm.listBox1 .Items .Add ("线程" threadh.ToString () "正在接收");

				}

				fs.Close();

				ns.Close ();

			}

			catch (Exception er)

			{

				MessageBox.Show (er.Message );

				fs.Close();

			}

			formm.listBox1 .Items.Add ("进程" threadh.ToString () "接收完毕!");

			formm.threadw[threadh]=true;

		}

	}
该类和Form1类处于统一命名空间,但不包含在Form1类中。下面定义“开始接收”按钮控件的事件响应函数:
		private void button1_Click(object sender, System.EventArgs e)

		{

			DateTime dt=DateTime.Now;//开始接收时间

			textBox1.Text =dt.ToString ();

			strurl=textBox2.Text .Trim ().ToString ();

			HttpWebRequest request;

			long filesize=0;

			try

			{

				request=(HttpWebRequest)HttpWebRequest.Create (strurl);

				filesize=request.GetResponse ().ContentLength;//取得目标文件的长度

				request.Abort ();

			}

			catch (Exception er)

			{

				MessageBox.Show (er.Message );

			}

			// 接收线程数

			thread=Convert.ToInt32 (textBox4.Text .Trim().ToString (),10);

			//根据线程数初始化数组

			threadw=new bool [thread];

			filenamew=new string [thread];

			filestartw=new int [thread];

			filesizew=new int[thread];

			

			//计算每个线程应该接收文件的大小

			int filethread=(int)filesize/thread;//平均分配

			int filethreade=filethread (int)filesize%thread;//剩余部分由最后一个线程完成

			//为数组赋值

			for (int i=0;i<thread;i  )

			{

				threadw[i]=false;//每个线程状态的初始值为假

				filenamew[i]=i.ToString () ".dat";//每个线程接收文件的临时文件名

				if (i<thread-1)

				{

				      filestartw[i]=filethread*i;//每个线程接收文件的起始点

				      filesizew[i]=filethread-1;//每个线程接收文件的长度

					

				}

				else

				{

					filestartw[i]=filethread*i;

					filesizew[i]=filethreade-1;

				}

			}

			//定义线程数组,启动接收线程

			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 ();

			}

			//启动合并各线程接收的文件线程

			Thread hbth=new Thread (new ThreadStart (hbfile));

			hbth.Start ();



		}
 
本文章更多内容<<上一页 - 1 - 2 - 3 - 下一页>>
相关文章

如何选中其他程序ListView控件中的某个Item
在VC中调用 WebService (非托管)
自动查找并删除VC生成的临时文件
一步步制作真彩工具条
如何定制浮动工具条
一个完善的ODBC数据库程序
一个简单的时钟程序
利用OpenGL实现动画效果
实现类似Excel和Visual C 里文件夹式样的标
深入浅出 CPropertySheet
如何提取网页中所有链接
DCT快速变换
think window procedure
关于CEdit控件的透明 --作者:monsoon
创建分层窗口,实现图像渐变
关于调试时输出的字符串信息
SendMessage和PostMessage,total ordering
用udp协议通讯示例
用VC 制作QQ自动登陆软件
MFC程序员的WTL指南: Part II - WTL 界面基

相关评论


本文章所属分类:首页 VC++

  热门关键字: