📄 p2psendimage.cs
字号:
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 P2pSendImage : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Panel panel1;
private System.ComponentModel.IContainer components;
private LanMsg.Controls.SockUDP sockUDP1;
private System.Windows.Forms.Timer timer1;
[Serializable]
private class sendFileInfo
{
public int MsgInfoClass=0;//文件发送消息类别
public long fileSize=0;//文件尺寸
public int pSendPos=0;//标记上次发送的位置
public byte[] FileBlock=null;//当前发送的文件块
public sendFileInfo( )
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public sendFileInfo(int msgInfoClass,long FileSize,int PSendPos,byte[] fileBlock )
{
MsgInfoClass=msgInfoClass;
fileSize=FileSize;
pSendPos=PSendPos;
FileBlock=fileBlock;
}
}
public P2pSendImage()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
try
{
this.fStream.Close();//关闭打开的文件资源
}
catch(Exception e){}
try
{
if((this.IsSendState && !this.sendOver) || !IsCancel)//如果文件正在传输中且没有成功便被用户关闭程序以强行终止,则给对方发送"取消文件传输"
this.sendData(new sendFileInfo(0,0,0,null));
}
catch(Exception e){}
try
{
this.sockUDP1.CloseSock();//关闭sockUDP1端口,清楚占用的资源
}
catch(Exception e){ }
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.panel1 = new System.Windows.Forms.Panel();
this.sockUDP1 = new LanMsg.Controls.SockUDP(this.components);
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// panel1
//
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(24, 24);
this.panel1.TabIndex = 0;
//
// sockUDP1
//
this.sockUDP1.Server = null;
this.sockUDP1.DataArrival += new LanMsg.Controls.SockUDP.DataArrivalEventHandler(this.sockUDP1_DataArrival);
//
// timer1
//
this.timer1.Interval = 500;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// P2pSendImage
//
this.BackColor = System.Drawing.Color.Cyan;
this.Controls.Add(this.panel1);
this.Name = "P2pSendImage";
this.Size = new System.Drawing.Size(24, 24);
this.ResumeLayout(false);
}
#endregion
#region 文件传输事件
public delegate void fileSendEndEventHandler(object sender,bool isSelf);//文件传输结束事件
public event fileSendEndEventHandler fileSendEnd;
public delegate void fileSendCancelEventHandler(object sender,bool isSelf);//取消文件传输事件
public event fileSendCancelEventHandler fileSendCancel;
#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 long FileSize=0;//文件尺寸
private string Extension="";//文件扩展名
private bool IsSendState=false;//标记文件是否在发送过程中
private int pSendPos=0;//标记上次发送的位置
private int buf=8000;//标记一次传输文件数据块的大小
private System.IO.FileStream fStream ;//文件操作流
private bool userCancelSend=false;//记录对方是否取消文件传输
private bool sendOver=false;//标记文件是否传输完成
private bool IsSend=false;//标识当前用户是发送文件还是接收文件
private bool IsCancel=false;//标识文件传输是点击“取消”而取消的
private int OutTime=0;//发送数据块超时读数器
public void SetParameter(bool isSend,string LabFileName,string fileName,long fileSize,System.Net.IPAddress ServerIP,int ServerPort,string fileExtension)
{
//文件传输前建立双方连接的参数设置函数
this.IsSend=isSend;
this.FileSize=fileSize;
this.serverIp=ServerIP;
this.serverPort=ServerPort;
this.Extension =fileExtension;
this.labFileName.Text =LabFileName;//文件名称
this.Listen();
this.timer1.Enabled=true;//保持UDP端口在外网上的映射
this.labelProgress.Text="(0/"+ this.FileSize.ToString() +")";
if(IsSend)
{
this.FileName=fileName;//文件的绝对路径
this.linkReceive.Visible=false;
this.linkSaveAs.Visible=false;
this.labOr.Visible=false;
this.labelState.Text="等待对方接收文件...";
}
else
{
this.labelState.Text="对方正等待您接收文件...";
this.sockUDP1.Send(this.serverIp,this.serverPort,new Controls.ClassSerializers().SerializeBinary(new sendFileInfo(100,0,0,null)).ToArray());
}
}
public string GetSizeStr(long fileSize)//获得传输文件的尺寸字符串
{
if(Convert.ToInt32(fileSize/1024)==0)
return fileSize.ToString() +"字节";
if(Convert.ToInt32(fileSize/1024)>0 && Convert.ToInt32(fileSize/(1024*1024))==0)
return Convert.ToInt32(fileSize/1024).ToString() +"KB";
if(Convert.ToInt32(fileSize/(1024*1024))>0 && Convert.ToInt32(fileSize/(1024*1024*1024))==0)
return Convert.ToInt32(fileSize/(1024*1024)).ToString() +"MB";
if(Convert.ToInt32(fileSize/(1024*1024*1024))>0)
return Convert.ToInt32(fileSize/(1024*1024*1024)).ToString() +"GB";
return "未知大小";
}
private void linkLabelCancel_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
if(MessageBox.Show("确定要终止文件的传输吗?","提示",System.Windows.Forms.MessageBoxButtons.YesNo,System.Windows.Forms.MessageBoxIcon.Question)==System.Windows.Forms.DialogResult.Yes )
{
if(!this.IsSendState)//如果当前还没有开始传输,则要发消息给对方,告诉对方已经取消文件传输
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -