⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p2pfilessend.cs

📁 该即时通讯系统系统能够实现像QQ一样的通讯功能
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
namespace LanMsg.Controls
{
	/// <summary>
	/// filesSend 的摘要说明。
	/// </summary>
	public class filesSend : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel panel4;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Panel panel3;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Panel panel5;
		private System.Windows.Forms.Panel panel6;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.LinkLabel linkLabelCancel;
		private System.Windows.Forms.Label labOr;
		private System.Windows.Forms.LinkLabel linkSaveAs;
		private LanMsg.Controls.SockUDP sockUDP1;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.Label labelState;
		private LanMsg.XpProgressBar.XpProgressBar PBar1;
		private System.Windows.Forms.Panel panel7;
		private System.Windows.Forms.PictureBox picWait;
		private System.Windows.Forms.Panel panel8;
		private System.Windows.Forms.Label labFileName;
		private System.Windows.Forms.Label labProcess;

		[Serializable]
		private class sendFileInfo
		{
			public int MsgInfoClass=0;//文件发送消息类别
			public int fileSize=0;//文件尺寸
			public int pSendPos=0;//标记上次发送的位置
			public byte[] FileBlock=null;//当前发送的文件块

			public sendFileInfo( )
			{
				//
				// TODO: 在此处添加构造函数逻辑
				//
			}

			public sendFileInfo(int msgInfoClass,int FileSize,int PSendPos,byte[] fileBlock )
			{
				MsgInfoClass=msgInfoClass;
				fileSize=FileSize;
				pSendPos=PSendPos;
				FileBlock=fileBlock;
			}
		}

		public filesSend()
		{
			// 该调用是 Windows.Forms 窗体设计器所必需的。
			InitializeComponent();
			// TODO: 在 InitializeComponent 调用后添加任何初始化
			this.fileSend +=new fileSendEventHandler(filesSend_fileSend);
		}

		/// <summary> 
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			try
			{
				this.fStream.Close();//关闭打开的文件资源
			}
			catch{}
			try
			{
				if((this.IsSendState && !this.sendOver) || !IsCancel)//如果文件正在传输中且没有成功便被用户关闭程序以强行终止,则给对方发送"取消文件传输"
			      this.sendData(new sendFileInfo(0,0,0,null));
			}
			catch{}
			try
			{
				this.sockUDP1.CloseSock();//关闭sockUDP1端口,清楚占用的资源 
			}
			catch{}

			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}


		#region 组件设计器生成的代码
		/// <summary> 
		/// 设计器支持所需的方法 - 不要使用代码编辑器 
		/// 修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(filesSend));
			this.panel1 = new System.Windows.Forms.Panel();
			this.panel6 = new System.Windows.Forms.Panel();
			this.labFileName = new System.Windows.Forms.Label();
			this.picWait = new System.Windows.Forms.PictureBox();
			this.panel7 = new System.Windows.Forms.Panel();
			this.panel5 = new System.Windows.Forms.Panel();
			this.label4 = new System.Windows.Forms.Label();
			this.linkLabelCancel = new System.Windows.Forms.LinkLabel();
			this.labOr = new System.Windows.Forms.Label();
			this.linkSaveAs = new System.Windows.Forms.LinkLabel();
			this.panel4 = new System.Windows.Forms.Panel();
			this.labProcess = new System.Windows.Forms.Label();
			this.PBar1 = new LanMsg.XpProgressBar.XpProgressBar();
			this.panel3 = new System.Windows.Forms.Panel();
			this.panel2 = new System.Windows.Forms.Panel();
			this.labelState = new System.Windows.Forms.Label();
			this.panel8 = new System.Windows.Forms.Panel();
			this.sockUDP1 = new LanMsg.Controls.SockUDP(this.components);
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.panel1.SuspendLayout();
			this.panel6.SuspendLayout();
			this.panel5.SuspendLayout();
			this.panel4.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.panel6);
			this.panel1.Controls.Add(this.panel5);
			this.panel1.Controls.Add(this.panel4);
			this.panel1.Controls.Add(this.labelState);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(280, 184);
			this.panel1.TabIndex = 0;
			// 
			// panel6
			// 
			this.panel6.Controls.Add(this.labFileName);
			this.panel6.Controls.Add(this.picWait);
			this.panel6.Controls.Add(this.panel7);
			this.panel6.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel6.Location = new System.Drawing.Point(0, 91);
			this.panel6.Name = "panel6";
			this.panel6.Size = new System.Drawing.Size(280, 93);
			this.panel6.TabIndex = 10;
			// 
			// labFileName
			// 
			this.labFileName.Dock = System.Windows.Forms.DockStyle.Fill;
			this.labFileName.Location = new System.Drawing.Point(32, 0);
			this.labFileName.Name = "labFileName";
			this.labFileName.Size = new System.Drawing.Size(248, 93);
			this.labFileName.TabIndex = 18;
			// 
			// picWait
			// 
			this.picWait.Dock = System.Windows.Forms.DockStyle.Left;
			this.picWait.Image = ((System.Drawing.Image)(resources.GetObject("picWait.Image")));
			this.picWait.Location = new System.Drawing.Point(4, 0);
			this.picWait.Name = "picWait";
			this.picWait.Size = new System.Drawing.Size(28, 93);
			this.picWait.TabIndex = 17;
			this.picWait.TabStop = false;
			// 
			// panel7
			// 
			this.panel7.Dock = System.Windows.Forms.DockStyle.Left;
			this.panel7.Location = new System.Drawing.Point(0, 0);
			this.panel7.Name = "panel7";
			this.panel7.Size = new System.Drawing.Size(4, 93);
			this.panel7.TabIndex = 16;
			// 
			// panel5
			// 
			this.panel5.Controls.Add(this.label4);
			this.panel5.Controls.Add(this.linkLabelCancel);
			this.panel5.Controls.Add(this.labOr);
			this.panel5.Controls.Add(this.linkSaveAs);
			this.panel5.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel5.Location = new System.Drawing.Point(0, 71);
			this.panel5.Name = "panel5";
			this.panel5.Size = new System.Drawing.Size(280, 20);
			this.panel5.TabIndex = 9;
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Dock = System.Windows.Forms.DockStyle.Left;
			this.label4.ForeColor = System.Drawing.Color.Purple;
			this.label4.Location = new System.Drawing.Point(125, 0);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(29, 17);
			this.label4.TabIndex = 22;
			this.label4.Text = "传输";
			this.label4.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
			this.label4.Visible = false;
			// 
			// linkLabelCancel
			// 
			this.linkLabelCancel.AutoSize = true;
			this.linkLabelCancel.Dock = System.Windows.Forms.DockStyle.Left;
			this.linkLabelCancel.ForeColor = System.Drawing.Color.Purple;
			this.linkLabelCancel.LinkColor = System.Drawing.Color.DarkOliveGreen;
			this.linkLabelCancel.Location = new System.Drawing.Point(71, 0);
			this.linkLabelCancel.Name = "linkLabelCancel";
			this.linkLabelCancel.Size = new System.Drawing.Size(54, 17);
			this.linkLabelCancel.TabIndex = 21;
			this.linkLabelCancel.TabStop = true;
			this.linkLabelCancel.Text = "取消传输";
			this.linkLabelCancel.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
			this.linkLabelCancel.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabelCancel_LinkClicked);
			// 
			// labOr
			// 
			this.labOr.AutoSize = true;
			this.labOr.Dock = System.Windows.Forms.DockStyle.Left;
			this.labOr.ForeColor = System.Drawing.Color.Purple;
			this.labOr.Location = new System.Drawing.Point(54, 0);
			this.labOr.Name = "labOr";
			this.labOr.Size = new System.Drawing.Size(17, 17);
			this.labOr.TabIndex = 20;
			this.labOr.Text = "或";
			this.labOr.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
			// 
			// linkSaveAs
			// 
			this.linkSaveAs.AutoSize = true;
			this.linkSaveAs.Dock = System.Windows.Forms.DockStyle.Left;
			this.linkSaveAs.ForeColor = System.Drawing.Color.Purple;
			this.linkSaveAs.LinkColor = System.Drawing.Color.DarkOliveGreen;
			this.linkSaveAs.Location = new System.Drawing.Point(0, 0);
			this.linkSaveAs.Name = "linkSaveAs";
			this.linkSaveAs.Size = new System.Drawing.Size(54, 17);
			this.linkSaveAs.TabIndex = 17;
			this.linkSaveAs.TabStop = true;
			this.linkSaveAs.Text = "接收文件";
			this.linkSaveAs.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
			this.linkSaveAs.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkSaveAs_LinkClicked);
			// 
			// panel4
			// 
			this.panel4.Controls.Add(this.labProcess);
			this.panel4.Controls.Add(this.PBar1);
			this.panel4.Controls.Add(this.panel3);
			this.panel4.Controls.Add(this.panel2);
			this.panel4.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel4.Location = new System.Drawing.Point(0, 24);
			this.panel4.Name = "panel4";
			this.panel4.Size = new System.Drawing.Size(280, 47);
			this.panel4.TabIndex = 8;
			// 
			// labProcess
			// 
			this.labProcess.Dock = System.Windows.Forms.DockStyle.Top;
			this.labProcess.ForeColor = System.Drawing.Color.DimGray;
			this.labProcess.Location = new System.Drawing.Point(8, 12);
			this.labProcess.Name = "labProcess";
			this.labProcess.Size = new System.Drawing.Size(264, 36);
			this.labProcess.TabIndex = 6;
			this.labProcess.Text = "(0/0)";
			this.labProcess.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// PBar1
			// 
			this.PBar1.ColorBackGround = System.Drawing.Color.White;
			this.PBar1.ColorBarBorder = System.Drawing.Color.FromArgb(((System.Byte)(170)), ((System.Byte)(240)), ((System.Byte)(170)));
			this.PBar1.ColorBarCenter = System.Drawing.Color.FromArgb(((System.Byte)(10)), ((System.Byte)(150)), ((System.Byte)(10)));
			this.PBar1.ColorText = System.Drawing.Color.Blue;
			this.PBar1.Dock = System.Windows.Forms.DockStyle.Top;
			this.PBar1.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.PBar1.Location = new System.Drawing.Point(8, 0);
			this.PBar1.Name = "PBar1";
			this.PBar1.Position = 0;
			this.PBar1.PositionMax = 100;
			this.PBar1.PositionMin = 0;
			this.PBar1.Size = new System.Drawing.Size(264, 12);
			this.PBar1.TabIndex = 5;
			// 
			// panel3
			// 
			this.panel3.Dock = System.Windows.Forms.DockStyle.Right;
			this.panel3.Location = new System.Drawing.Point(272, 0);
			this.panel3.Name = "panel3";
			this.panel3.Size = new System.Drawing.Size(8, 47);
			this.panel3.TabIndex = 3;
			// 
			// panel2
			// 
			this.panel2.Dock = System.Windows.Forms.DockStyle.Left;
			this.panel2.Location = new System.Drawing.Point(0, 0);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(8, 47);
			this.panel2.TabIndex = 2;
			// 
			// labelState
			// 
			this.labelState.BackColor = System.Drawing.Color.Transparent;
			this.labelState.Dock = System.Windows.Forms.DockStyle.Top;
			this.labelState.ForeColor = System.Drawing.Color.Purple;
			this.labelState.Location = new System.Drawing.Point(0, 0);
			this.labelState.Name = "labelState";
			this.labelState.Size = new System.Drawing.Size(280, 24);
			this.labelState.TabIndex = 0;
			this.labelState.Text = "等待接收...";
			this.labelState.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// panel8
			// 
			this.panel8.Location = new System.Drawing.Point(0, 0);
			this.panel8.Name = "panel8";
			this.panel8.TabIndex = 0;
			// 
			// sockUDP1
			// 
			this.sockUDP1.Server = ((System.Net.IPEndPoint)(resources.GetObject("sockUDP1.Server")));
			this.sockUDP1.DataArrival += new LanMsg.Controls.SockUDP.DataArrivalEventHandler(this.sockUDP1_DataArrival);
			// 
			// timer1
			// 
			this.timer1.Enabled = true;
			this.timer1.Interval = 50000;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// filesSend
			// 
			this.BackColor = System.Drawing.Color.Cyan;
			this.Controls.Add(this.panel1);
			this.Name = "filesSend";
			this.Size = new System.Drawing.Size(280, 184);
			this.panel1.ResumeLayout(false);
			this.panel6.ResumeLayout(false);
			this.panel5.ResumeLayout(false);
			this.panel4.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#region  文件传输事件
	
		public class FileSendEventArgs:System.EventArgs
		{
			public bool isSelf ;
			public string fileName;
			public FileSendEventArgs(bool IsSelf,string FileName)
			{
               this.isSelf=IsSelf;
			   this.fileName =FileName;
			}
		}
		    public delegate void fileSendEndEventHandler(object sender,FileSendEventArgs e);//文件传输结束事件
			public  event fileSendEndEventHandler fileSendEnd; 

			public delegate void fileSendCancelEventHandler(object sender,FileSendEventArgs e);//取消文件传输事件
			public  event fileSendCancelEventHandler fileSendCancel; 
	    
			public delegate void fileSendEventHandler(object sender,FileSendEventArgs e);//接收文件传输事件
			public  event fileSendEventHandler fileSend; 

			public delegate void fileSendOutTimeEventHandler(object sender,FileSendEventArgs e);//接收文件发送超时
			public  event fileSendOutTimeEventHandler fileSendOutTime; 

		#endregion


		public string FileName=Application.StartupPath + @"\ReceiveFiles\";//发送或接收文件所保存的位置
		
		private System.Net.IPAddress serverIp=System.Net.IPAddress.Parse("127.0.0.1");//对方ip地址 

		private int serverPort=0;//对方Ip端口

		private int FileSize=0;//文件尺寸
	
		private string Extension="";//文件扩展名

		private bool IsSendState=false;//标记文件是否在发送过程中

		private int  pSendPos=0;//标记上次发送的位置

		private System.IO.FileStream fStream ;//文件操作流

		private bool userCancelSend=false;//记录对方是否取消文件传输

		private bool sendOver=false;//标记文件是否传输完成
       
		private bool IsSend=false;//标识当前用户是发送文件还是接收文件

		private bool IsCancel=false;//标识文件传输是点击“取消”而取消的

		private int ReadCount=0;//记录当前读取文件所获得的字节数

		private int currSendPos=0;//设置当前发送的文件块的终点位置

		private string fileSizeStr="0";

		private int _buf=8000;//标记一次传输文件数据块的大小,不能超过MTU限制,否则在因特网上的数据发送将不成功00
		[Category("全局设置")]
		[Description("设置UDP每一次传输数据包的大小")]
		[DefaultValue(8000)]
		public int buf 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -