文件传输协议.docx

上传人:b****8 文档编号:28861307 上传时间:2023-07-20 格式:DOCX 页数:34 大小:21.06KB
下载 相关 举报
文件传输协议.docx_第1页
第1页 / 共34页
文件传输协议.docx_第2页
第2页 / 共34页
文件传输协议.docx_第3页
第3页 / 共34页
文件传输协议.docx_第4页
第4页 / 共34页
文件传输协议.docx_第5页
第5页 / 共34页
点击查看更多>>
下载资源
资源描述

文件传输协议.docx

《文件传输协议.docx》由会员分享,可在线阅读,更多相关《文件传输协议.docx(34页珍藏版)》请在冰豆网上搜索。

文件传输协议.docx

文件传输协议

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Net;

usingSystem.Threading;

usingSystem.Net.Sockets;

usingSystem.IO;

namespaceTestSocketServerHSTF

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

//不显示出dataGridView1的最后一行空白

dataGridView1.AllowUserToAddRows=false;

}

#region定义变量

#endregion

#region进入窗体即启动服务

privatevoidForm1_Load(objectsender,EventArgse)

{

//开启接收线程

ThreadTempThread=newThread(newThreadStart(this.StartReceive));

TempThread.Start();

}

#endregion

#region功能函数

privatevoidStartReceive()

{

//创建一个网络端点

IPEndPointipep=newIPEndPoint(IPAddress.Any,int.Parse("2005"));

//MessageBox.Show(IPAddress.Any);

//创建一个套接字

Socketserver=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

//绑定套接字到端口

server.Bind(ipep);

//开始侦听(并堵塞该线程)

server.Listen(10);

//确认连接

Socketclient=server.Accept();

//获得客户端节点对象

IPEndPointclientep=(IPEndPoint)client.RemoteEndPoint;

//获得[文件名]

stringSendFileName=System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));

//MessageBox.Show("文件名"+SendFileName);

//获得[包的大小]

stringbagSize=System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));

//MessageBox.Show("包大小"+bagSize);

//获得[包的总数量]

intbagCount=int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));

//MessageBox.Show("包的总数量"+bagCount);

//获得[最后一个包的大小]

stringbagLast=System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));

//MessageBox.Show("最后一个包的大小"+bagLast);

//创建一个新文件

FileStreamMyFileStream=newFileStream(SendFileName,FileMode.Create,FileAccess.Write);

//已发送包的个数

intSendedCount=0;

while(true)

{

byte[]data=TransferFiles.ReceiveVarData(client);

if(data.Length==0)

{

break;

}

else

{

SendedCount++;

//将接收到的数据包写入到文件流对象

MyFileStream.Write(data,0,data.Length);

//显示已发送包的个数

//MessageBox.Show("已发送包个数"+SendedCount.ToString());

}

}

//关闭文件流

MyFileStream.Close();

//关闭套接字

client.Close();

//填加到dgv里

//文件大小,IP,已发送包的个数,文件名,包的总量,最后一个包的大小

this.dataGridView1.Rows.Add(bagSize,clientep.Address,SendedCount,SendFileName,bagCount,bagLast);

//MessageBox.Show("文件接收完毕!

");

}

#endregion

#region拦截Windows消息,关闭窗体时执行

protectedoverridevoidWndProc(refMessagem)

{

constintWM_SYSCOMMAND=0x0112;

constintSC_CLOSE=0xF060;

if(m.Msg==WM_SYSCOMMAND&&(int)m.WParam==SC_CLOSE)

{//捕捉关闭窗体消息

//Userclickedclosebutton

//this.WindowState=FormWindowState.Minimized;//把右上角红叉关闭按钮变最小化

ServiceStop();

}

base.WndProc(refm);

}

#endregion

#region停止服务

//停止服务

privatevoidServiceStop()

{

try

{

}

catch{}

try

{

}

catch{}

}

#endregion

}

}

客户端用来发送文件,选择文件后点发送按钮发送文件

客户端代码:

////////////////////////////////////////////////////////////////////////////////

//title:

点对点文件传输程序//

////////////////////////////////////////////////////////////////////////////////

//////////////////////////Begin-发送端//////////////////////////////////

usingSystem;

usingSystem.Drawing;

usingSystem.Collections;

usingSystem.ComponentModel;

usingSystem.Windows.Forms;

usingSystem.Data;

usingSystem.IO;

usingSystem.Net;

usingSystem.Net.Sockets;

usingSystem.Threading;

namespace发送端

{

///

///Form1的摘要说明。

///

publicclassForm1:

System.Windows.Forms.Form

{

privateSystem.Windows.Forms.GroupBoxgroupBox1;

privateSystem.Windows.Forms.OpenFileDialogopenFileDialog1;

privateSystem.Windows.Forms.TextBoxtextBox1;

privateSystem.Windows.Forms.Buttonbutton1;

privateSystem.Windows.Forms.Labellabel1;

privateSystem.Windows.Forms.TextBoxtextBox2;

privateSystem.Windows.Forms.Labellabel2;

privateSystem.Windows.Forms.TextBoxtextBox3;

privateSystem.Windows.Forms.GroupBoxgroupBox2;

privateSystem.Windows.Forms.Labellabel3;

privateSystem.Windows.Forms.TextBoxtextBox4;

privateSystem.Windows.Forms.Labellabel4;

privateSystem.Windows.Forms.TextBoxtextBox5;

privateSystem.Windows.Forms.GroupBoxgroupBox3;

privateSystem.Windows.Forms.GroupBoxgroupBox4;

privateSystem.Windows.Forms.Buttonbutton2;

privateSystem.Windows.Forms.Labellabel5;

privateSystem.Windows.Forms.TextBoxtextBox6;

privateSystem.Windows.Forms.Labellabel6;

privateSystem.Windows.Forms.Labellabel7;

privateSystem.Windows.Forms.ProgressBarprogressBar1;

privateSystem.Windows.Forms.TextBoxtextBox7;

privateSystem.Windows.Forms.Labellabel8;

privateSystem.Windows.Forms.Labellabel9;

privateSystem.Windows.Forms.TextBoxtextBox8;

privateSystem.Windows.Forms.Labellabel10;

privateSystem.Windows.Forms.TextBoxtextBox9;

privateSystem.Windows.Forms.Labellabel11;

privateSystem.Windows.Forms.Labellabel12;

privateSystem.Windows.Forms.TextBoxtextBox10;

///

///必需的设计器变量。

///

privateSystem.ComponentModel.Containercomponents=null;

publicForm1()

{

//

//Windows窗体设计器支持所必需的

//

InitializeComponent();

//

//TODO:

在InitializeComponent调用后添加任何构造函数代码

//

}

///

///清理所有正在使用的资源。

///

protectedoverridevoidDispose(booldisposing)

{

if(disposing)

{

if(components!

=null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

#regionWindows窗体设计器生成的代码

///

///设计器支持所需的方法-不要使用代码编辑器修改

///此方法的内容。

///

privatevoidInitializeComponent()

{

this.groupBox1=newSystem.Windows.Forms.GroupBox();

this.textBox2=newSystem.Windows.Forms.TextBox();

this.textBox3=newSystem.Windows.Forms.TextBox();

this.label2=newSystem.Windows.Forms.Label();

this.label1=newSystem.Windows.Forms.Label();

this.button1=newSystem.Windows.Forms.Button();

this.textBox1=newSystem.Windows.Forms.TextBox();

this.label6=newSystem.Windows.Forms.Label();

this.openFileDialog1=newSystem.Windows.Forms.OpenFileDialog();

this.groupBox2=newSystem.Windows.Forms.GroupBox();

this.textBox6=newSystem.Windows.Forms.TextBox();

this.textBox5=newSystem.Windows.Forms.TextBox();

this.label4=newSystem.Windows.Forms.Label();

this.textBox4=newSystem.Windows.Forms.TextBox();

this.label3=newSystem.Windows.Forms.Label();

this.label5=newSystem.Windows.Forms.Label();

this.label9=newSystem.Windows.Forms.Label();

this.groupBox3=newSystem.Windows.Forms.GroupBox();

this.textBox8=newSystem.Windows.Forms.TextBox();

this.textBox9=newSystem.Windows.Forms.TextBox();

this.textBox7=newSystem.Windows.Forms.TextBox();

this.progressBar1=newSystem.Windows.Forms.ProgressBar();

this.label7=newSystem.Windows.Forms.Label();

this.label8=newSystem.Windows.Forms.Label();

this.label10=newSystem.Windows.Forms.Label();

this.label11=newSystem.Windows.Forms.Label();

this.label12=newSystem.Windows.Forms.Label();

this.textBox10=newSystem.Windows.Forms.TextBox();

this.groupBox4=newSystem.Windows.Forms.GroupBox();

this.button2=newSystem.Windows.Forms.Button();

this.groupBox1.SuspendLayout();

this.groupBox2.SuspendLayout();

this.groupBox3.SuspendLayout();

this.groupBox4.SuspendLayout();

this.SuspendLayout();

//

//groupBox1

//

this.groupBox1.Controls.Add(this.textBox2);

this.groupBox1.Controls.Add(this.textBox3);

this.groupBox1.Controls.Add(this.label2);

this.groupBox1.Controls.Add(this.label1);

this.groupBox1.Controls.Add(this.button1);

this.groupBox1.Controls.Add(this.textBox1);

this.groupBox1.Controls.Add(this.label6);

this.groupBox1.Location=newSystem.Drawing.Point(0,0);

this.groupBox1.Name="groupBox1";

this.groupBox1.Size=newSystem.Drawing.Size(416,96);

this.groupBox1.TabIndex=0;

this.groupBox1.TabStop=false;

this.groupBox1.Text="文件信息";

//

//textBox2

//

this.textBox2.Location=newSystem.Drawing.Point(80,40);

this.textBox2.Name="textBox2";

this.textBox2.ReadOnly=true;

this.textBox2.Size=newSystem.Drawing.Size(232,21);

this.textBox2.TabIndex=3;

//

//textBox3

//

this.textBox3.Location=newSystem.Drawing.Point(80,64);

this.textBox3.Name="textBox3";

this.textBox3.ReadOnly=true;

this.textBox3.Size=newSystem.Drawing.Size(136,21);

this.textBox3.TabIndex=3;

//

//label2

//

this.label2.Location=newSystem.Drawing.Point(8,72);

this.label2.Name="label2";

this.label2.Size=newSystem.Drawing.Size(100,16);

this.label2.TabIndex=4;

this.label2.Text="文件大小:

";

//

//label1

//

this.label1.Location=newSystem.Drawing.Point(16,48);

this.label1.Name="label1";

this.label1.Size=newSystem.Drawing.Size(96,16);

this.label1.TabIndex=2;

this.label1.Text="文件名:

";

//

//button1

//

this.button1.Location=newSystem.Drawing.Point(320,16);

this.button1.Name="button1";

this.button1.Size=newSystem.Drawing.Size(88,23);

this.button1.TabIndex=1;

this.button1.Text="浏览";

this.button1.Click+=newSystem.EventHandler(this.button1_Click);

//

//textBox1

//

this.textBox1.Location=newSystem.Drawing.Point(8,16);

this.textBox1.Name="textBox1";

this.textBox1.ReadOnly=true;

this.textBox1.Size=newSystem.Drawing.Size(304,21);

this.textBox1.TabIn

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 英语考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1