欢迎光临
我们一直在努力

亿众国际点对点文件传输程序-.NET教程,文件IO

建站超值云服务器,限时71元/月

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

//author: stardicky //

//e-mail: stardicky@hotmail.com //

//qqnumber: 9531511 //

//companyname: ezone international //

//class: hbs-0308 //

//title: 亿众国际点对点文件传输程序 //

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

//////////////////////////begin-亿众国际发送端//////////////////////////////////

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.data;

using system.io;

using system.net;

using system.net.sockets;

using system.threading;

namespace 亿众国际发送端

{

/// <summary>

/// form1 的摘要说明。

/// </summary>

public class ezonesend : system.windows.forms.form

{

private system.windows.forms.groupbox groupbox1;

private system.windows.forms.openfiledialog openfiledialog1;

private system.windows.forms.textbox textbox1;

private system.windows.forms.button button1;

private system.windows.forms.label label1;

private system.windows.forms.textbox textbox2;

private system.windows.forms.label label2;

private system.windows.forms.textbox textbox3;

private system.windows.forms.groupbox groupbox2;

private system.windows.forms.label label3;

private system.windows.forms.textbox textbox4;

private system.windows.forms.label label4;

private system.windows.forms.textbox textbox5;

private system.windows.forms.groupbox groupbox3;

private system.windows.forms.groupbox groupbox4;

private system.windows.forms.button button2;

private system.windows.forms.label label5;

private system.windows.forms.textbox textbox6;

private system.windows.forms.label label6;

private system.windows.forms.label label7;

private system.windows.forms.progressbar progressbar1;

private system.windows.forms.textbox textbox7;

private system.windows.forms.label label8;

private system.windows.forms.label label9;

private system.windows.forms.textbox textbox8;

private system.windows.forms.label label10;

private system.windows.forms.textbox textbox9;

private system.windows.forms.label label11;

private system.windows.forms.label label12;

private system.windows.forms.textbox textbox10;

/// <summary>

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

/// </summary>

private system.componentmodel.container components = null;

public ezonesend()

{

//

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

//

initializecomponent();

//

// todo: 在 initializecomponent 调用后添加任何构造函数代码

//

}

/// <summary>

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

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}

#region windows 窗体设计器生成的代码

/// <summary>

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

/// 此方法的内容。

/// </summary>

private void initializecomponent()

{

this.groupbox1 = new system.windows.forms.groupbox();

this.openfiledialog1 = new system.windows.forms.openfiledialog();

this.textbox1 = new system.windows.forms.textbox();

this.button1 = new system.windows.forms.button();

this.label1 = new system.windows.forms.label();

this.textbox2 = new system.windows.forms.textbox();

this.label2 = new system.windows.forms.label();

this.textbox3 = new system.windows.forms.textbox();

this.groupbox2 = new system.windows.forms.groupbox();

this.label3 = new system.windows.forms.label();

this.textbox4 = new system.windows.forms.textbox();

this.label4 = new system.windows.forms.label();

this.textbox5 = new system.windows.forms.textbox();

this.groupbox3 = new system.windows.forms.groupbox();

this.groupbox4 = new system.windows.forms.groupbox();

this.button2 = new system.windows.forms.button();

this.label5 = new system.windows.forms.label();

this.textbox6 = new system.windows.forms.textbox();

this.label6 = new system.windows.forms.label();

this.label7 = new system.windows.forms.label();

this.progressbar1 = new system.windows.forms.progressbar();

this.textbox7 = new system.windows.forms.textbox();

this.label8 = new system.windows.forms.label();

this.label9 = new system.windows.forms.label();

this.textbox8 = new system.windows.forms.textbox();

this.label10 = new system.windows.forms.label();

this.textbox9 = new system.windows.forms.textbox();

this.label11 = new system.windows.forms.label();

this.label12 = new system.windows.forms.label();

this.textbox10 = new system.windows.forms.textbox();

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 = new system.drawing.point(0, 0);

this.groupbox1.name = "groupbox1";

this.groupbox1.size = new system.drawing.size(416, 96);

this.groupbox1.tabindex = 0;

this.groupbox1.tabstop = false;

this.groupbox1.text = "文件信息";

//

// openfiledialog1

//

this.openfiledialog1.filter = "所有文件|*.*";

//

// textbox1

//

this.textbox1.location = new system.drawing.point(8, 16);

this.textbox1.name = "textbox1";

this.textbox1.readonly = true;

this.textbox1.size = new system.drawing.size(304, 21);

this.textbox1.tabindex = 0;

this.textbox1.text = "";

//

// button1

//

this.button1.location = new system.drawing.point(320, 16);

this.button1.name = "button1";

this.button1.size = new system.drawing.size(88, 23);

this.button1.tabindex = 1;

this.button1.text = "浏览";

this.button1.click += new system.eventhandler(this.button1_click);

//

// label1

//

this.label1.location = new system.drawing.point(16, 48);

this.label1.name = "label1";

this.label1.size = new system.drawing.size(96, 16);

this.label1.tabindex = 2;

this.label1.text = "文件名:";

//

// textbox2

//

this.textbox2.location = new system.drawing.point(80, 40);

this.textbox2.name = "textbox2";

this.textbox2.readonly = true;

this.textbox2.size = new system.drawing.size(232, 21);

this.textbox2.tabindex = 3;

this.textbox2.text = "";

//

// label2

//

this.label2.location = new system.drawing.point(8, 72);

this.label2.name = "label2";

this.label2.size = new system.drawing.size(100, 16);

this.label2.tabindex = 4;

this.label2.text = "文件大小:";

//

// textbox3

//

this.textbox3.location = new system.drawing.point(80, 64);

this.textbox3.name = "textbox3";

this.textbox3.readonly = true;

this.textbox3.size = new system.drawing.size(136, 21);

this.textbox3.tabindex = 3;

this.textbox3.text = "";

//

// groupbox2

//

this.groupbox2.controls.add(this.textbox6);

this.groupbox2.controls.add(this.textbox5);

this.groupbox2.controls.add(this.label4);

this.groupbox2.controls.add(this.textbox4);

this.groupbox2.controls.add(this.label3);

this.groupbox2.controls.add(this.label5);

this.groupbox2.controls.add(this.label9);

this.groupbox2.location = new system.drawing.point(0, 96);

this.groupbox2.name = "groupbox2";

this.groupbox2.size = new system.drawing.size(416, 72);

this.groupbox2.tabindex = 1;

this.groupbox2.tabstop = false;

this.groupbox2.text = "系统设置";

//

// label3

//

this.label3.location = new system.drawing.point(16, 24);

this.label3.name = "label3";

this.label3.size = new system.drawing.size(100, 16);

this.label3.tabindex = 0;

this.label3.text = "本机ip地址:";

//

// textbox4

//

this.textbox4.location = new system.drawing.point(96, 16);

this.textbox4.name = "textbox4";

this.textbox4.readonly = true;

this.textbox4.size = new system.drawing.size(144, 21);

this.textbox4.tabindex = 1;

this.textbox4.text = "";

//

// label4

//

this.label4.location = new system.drawing.point(256, 24);

this.label4.name = "label4";

this.label4.size = new system.drawing.size(100, 16);

this.label4.tabindex = 2;

this.label4.text = "传输端口:";

//

// textbox5

//

this.textbox5.location = new system.drawing.point(320, 16);

this.textbox5.name = "textbox5";

this.textbox5.size = new system.drawing.size(80, 21);

this.textbox5.tabindex = 3;

this.textbox5.text = "8888";

//

// groupbox3

//

this.groupbox3.controls.add(this.textbox8);

this.groupbox3.controls.add(this.textbox9);

this.groupbox3.controls.add(this.textbox7);

this.groupbox3.controls.add(this.progressbar1);

this.groupbox3.controls.add(this.label7);

this.groupbox3.controls.add(this.label8);

this.groupbox3.controls.add(this.label10);

this.groupbox3.controls.add(this.label11);

this.groupbox3.controls.add(this.label12);

this.groupbox3.controls.add(this.textbox10);

this.groupbox3.location = new system.drawing.point(0, 168);

this.groupbox3.name = "groupbox3";

this.groupbox3.size = new system.drawing.size(416, 168);

this.groupbox3.tabindex = 2;

this.groupbox3.tabstop = false;

this.groupbox3.text = "状态信息";

//

// groupbox4

//

this.groupbox4.controls.add(this.button2);

this.groupbox4.location = new system.drawing.point(0, 336);

this.groupbox4.name = "groupbox4";

this.groupbox4.size = new system.drawing.size(416, 48);

this.groupbox4.tabindex = 3;

this.groupbox4.tabstop = false;

this.groupbox4.text = "系统控制";

//

// button2

//

this.button2.location = new system.drawing.point(16, 16);

this.button2.name = "button2";

this.button2.tabindex = 0;

this.button2.text = "开始发送";

this.button2.click += new system.eventhandler(this.button2_click);

//

// label5

//

this.label5.location = new system.drawing.point(24, 48);

this.label5.name = "label5";

this.label5.size = new system.drawing.size(88, 16);

this.label5.tabindex = 2;

this.label5.text = "包的大小:";

//

// textbox6

//

this.textbox6.location = new system.drawing.point(96, 40);

this.textbox6.name = "textbox6";

this.textbox6.size = new system.drawing.size(72, 21);

this.textbox6.tabindex = 3;

this.textbox6.text = "50000";

//

// label6

//

this.label6.location = new system.drawing.point(224, 72);

this.label6.name = "label6";

this.label6.size = new system.drawing.size(96, 16);

this.label6.tabindex = 2;

this.label6.text = "(单位:字节)";

//

// label7

//

this.label7.location = new system.drawing.point(32, 24);

this.label7.name = "label7";

this.label7.size = new system.drawing.size(96, 16);

this.label7.tabindex = 2;

this.label7.text = "接收端ip地址:";

//

// progressbar1

//

this.progressbar1.location = new system.drawing.point(8, 136);

this.progressbar1.name = "progressbar1";

this.progressbar1.size = new system.drawing.size(400, 23);

this.progressbar1.step = 1;

this.progressbar1.tabindex = 3;

//

// textbox7

//

this.textbox7.location = new system.drawing.point(120, 16);

this.textbox7.name = "textbox7";

this.textbox7.readonly = true;

this.textbox7.size = new system.drawing.size(160, 21);

this.textbox7.tabindex = 1;

this.textbox7.text = "";

//

// label8

//

this.label8.location = new system.drawing.point(40, 48);

this.label8.name = "label8";

this.label8.size = new system.drawing.size(80, 16);

this.label8.tabindex = 2;

this.label8.text = "包的总数量:";

//

// label9

//

this.label9.location = new system.drawing.point(176, 48);

this.label9.name = "label9";

this.label9.size = new system.drawing.size(224, 16);

this.label9.tabindex = 2;

this.label9.text = "(范围:10000 – 60000 单位:字节)";

//

// textbox8

//

this.textbox8.location = new system.drawing.point(120, 40);

this.textbox8.name = "textbox8";

this.textbox8.readonly = true;

this.textbox8.size = new system.drawing.size(160, 21);

this.textbox8.tabindex = 1;

this.textbox8.text = "";

//

// label10

//

this.label10.location = new system.drawing.point(8, 72);

this.label10.name = "label10";

this.label10.size = new system.drawing.size(120, 16);

this.label10.tabindex = 2;

this.label10.text = "最后一个包的大小:";

//

// textbox9

//

this.textbox9.location = new system.drawing.point(120, 64);

this.textbox9.name = "textbox9";

this.textbox9.readonly = true;

this.textbox9.size = new system.drawing.size(80, 21);

this.textbox9.tabindex = 1;

this.textbox9.text = "";

//

// label11

//

this.label11.location = new system.drawing.point(200, 72);

this.label11.name = "label11";

this.label11.size = new system.drawing.size(96, 16);

this.label11.tabindex = 2;

this.label11.text = "(单位:字节)";

//

// label12

//

this.label12.location = new system.drawing.point(16, 96);

this.label12.name = "label12";

this.label12.size = new system.drawing.size(104, 16);

this.label12.tabindex = 2;

this.label12.text = "已发送包的数量:";

//

// textbox10

//

this.textbox10.location = new system.drawing.point(120, 88);

this.textbox10.name = "textbox10";

this.textbox10.readonly = true;

this.textbox10.size = new system.drawing.size(80, 21);

this.textbox10.tabindex = 1;

this.textbox10.text = "";

//

// ezonesend

//

this.autoscalebasesize = new system.drawing.size(6, 14);

this.clientsize = new system.drawing.size(416, 389);

this.controls.add(this.groupbox4);

this.controls.add(this.groupbox3);

this.controls.add(this.groupbox2);

this.controls.add(this.groupbox1);

this.formborderstyle = system.windows.forms.formborderstyle.fixedsingle;

this.maximizebox = false;

this.name = "ezonesend";

this.text = "亿众国际点对点文件传输软体发送端";

this.load += new system.eventhandler(this.ezonesend_load);

this.groupbox1.resumelayout(false);

this.groupbox2.resumelayout(false);

this.groupbox3.resumelayout(false);

this.groupbox4.resumelayout(false);

this.resumelayout(false);

}

#endregion

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[stathread]

static void main()

{

application.run(new ezonesend());

}

private void button1_click(object sender, system.eventargs e)

{

//选择要进行传输的文件

if(this.openfiledialog1.showdialog()==dialogresult.ok)

{

fileinfo ezonefile=new fileinfo(this.openfiledialog1.filename);

this.textbox1.text=ezonefile.fullname;

this.textbox2.text=ezonefile.name;

this.textbox3.text=ezonefile.length.tostring();

}

}

private void startsend()

{

//创建一个文件对象

fileinfo ezonefile=new fileinfo(this.textbox1.text);

//打开文件流

filestream ezonestream=ezonefile.openread();

//包的大小

int packetsize=int.parse(this.textbox6.text);

//包的数量

int packetcount=(int)(ezonestream.length/((long)packetsize));

this.textbox8.text=packetcount.tostring();

this.progressbar1.maximum=packetcount;

//最后一个包的大小

int lastdatapacket=(int)(ezonestream.length-((long)(packetsize*packetcount)));

this.textbox9.text=lastdatapacket.tostring();

//创建一个网络端点

ipendpoint ipep=new ipendpoint(ipaddress.any,int.parse(this.textbox5.text));

//创建一个套接字

socket server=new socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp);

//绑定套接字到端口

server.bind(ipep);

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

server.listen(10);

//确认连接

socket client=server.accept();

//获得客户端节点对象

ipendpoint clientep=(ipendpoint)client.remoteendpoint;

//获得客户端的ip地址

this.textbox7.text=clientep.address.tostring();

//发送[文件名]到客户端

commonmodule.ezonemodule.sendvardata(client,system.text.encoding.unicode.getbytes(ezonefile.name));

//发送[包的大小]到客户端

commonmodule.ezonemodule.sendvardata(client,system.text.encoding.unicode.getbytes(packetsize.tostring()));

//发送[包的总数量]到客户端

commonmodule.ezonemodule.sendvardata(client,system.text.encoding.unicode.getbytes(packetcount.tostring()));

//发送[最后一个包的大小]到客户端

commonmodule.ezonemodule.sendvardata(client,system.text.encoding.unicode.getbytes(lastdatapacket.tostring()));

//数据包

byte[] data=new byte[packetsize];

//开始循环发送数据包

for(int i=0;i<packetcount;i++)

{

//从文件流读取数据并填充数据包

ezonestream.read(data,0,data.length);

//发送数据包

commonmodule.ezonemodule.sendvardata(client,data);

//显示发送数据包的个数

this.textbox10.text=((int)(i+1)).tostring();

//进度条值的显示

this.progressbar1.performstep();

}

//如果还有多余的数据包,则应该发送完毕!

if(lastdatapacket!=0)

{

data=new byte[lastdatapacket];

ezonestream.read(data,0,data.length);

commonmodule.ezonemodule.sendvardata(client,data);

this.progressbar1.value=this.progressbar1.maximum;

}

//关闭套接字

client.close();

server.close();

//关闭文件流

ezonestream.close();

this.button2.enabled=true;

messagebox.show("文件传输完毕!");

}

private void button2_click(object sender, system.eventargs e)

{

//开启文件传输子线程

thread tempthread=new thread(new threadstart(this.startsend));

tempthread.start();

this.button2.enabled=false;

}

private void ezonesend_load(object sender, system.eventargs e)

{

//获得本机的ip地址

this.textbox4.text=dns.gethostbyname(dns.gethostname()).addresslist[0].tostring();

}

}

}

//////////////////////////end-亿众国际发送端////////////////////////////////////

//////////////////////////begin-亿众国际接收端//////////////////////////////////

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.data;

using system.net;

using system.net.sockets;

using system.io;

using system.threading;

namespace 亿众国际接收端

{

/// <summary>

/// form1 的摘要说明。

/// </summary>

public class ezonereceive : system.windows.forms.form

{

private system.windows.forms.groupbox groupbox1;

private system.windows.forms.button button1;

private system.windows.forms.groupbox groupbox3;

private system.windows.forms.groupbox groupbox2;

private system.windows.forms.textbox textbox5;

private system.windows.forms.label label4;

private system.windows.forms.textbox textbox4;

private system.windows.forms.label label3;

private system.windows.forms.textbox textbox1;

private system.windows.forms.label label1;

private system.windows.forms.label label2;

private system.windows.forms.textbox textbox2;

private system.windows.forms.textbox textbox8;

private system.windows.forms.textbox textbox9;

private system.windows.forms.label label8;

private system.windows.forms.label label10;

private system.windows.forms.label label11;

private system.windows.forms.label label12;

private system.windows.forms.textbox textbox10;

private system.windows.forms.progressbar progressbar1;

private system.windows.forms.textbox textbox3;

private system.windows.forms.label label5;

private system.windows.forms.label label6;

/// <summary>

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

/// </summary>

private system.componentmodel.container components = null;

public ezonereceive()

{

//

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

//

initializecomponent();

//

// todo: 在 initializecomponent 调用后添加任何构造函数代码

//

}

/// <summary>

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

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}

#region windows 窗体设计器生成的代码

/// <summary>

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

/// 此方法的内容。

/// </summary>

private void initializecomponent()

{

this.groupbox1 = new system.windows.forms.groupbox();

this.button1 = new system.windows.forms.button();

this.groupbox3 = new system.windows.forms.groupbox();

this.groupbox2 = new system.windows.forms.groupbox();

this.textbox5 = new system.windows.forms.textbox();

this.label4 = new system.windows.forms.label();

this.textbox4 = new system.windows.forms.textbox();

this.label3 = new system.windows.forms.label();

this.textbox1 = new system.windows.forms.textbox();

this.label1 = new system.windows.forms.label();

this.label2 = new system.windows.forms.label();

this.textbox2 = new system.windows.forms.textbox();

this.textbox8 = new system.windows.forms.textbox();

this.textbox9 = new system.windows.forms.textbox();

this.label8 = new system.windows.forms.label();

this.label10 = new system.windows.forms.label();

this.label11 = new system.windows.forms.label();

this.label12 = new system.windows.forms.label();

this.textbox10 = new system.windows.forms.textbox();

this.progressbar1 = new system.windows.forms.progressbar();

this.textbox3 = new system.windows.forms.textbox();

this.label5 = new system.windows.forms.label();

this.label6 = new system.windows.forms.label();

this.groupbox1.suspendlayout();

this.groupbox3.suspendlayout();

this.groupbox2.suspendlayout();

this.suspendlayout();

//

// groupbox1

//

this.groupbox1.controls.add(this.button1);

this.groupbox1.location = new system.drawing.point(0, 256);

this.groupbox1.name = "groupbox1";

this.groupbox1.size = new system.drawing.size(408, 48);

this.groupbox1.tabindex = 0;

this.groupbox1.tabstop = false;

this.groupbox1.text = "系统控制";

//

// button1

//

this.button1.location = new system.drawing.point(8, 16);

this.button1.name = "button1";

this.button1.tabindex = 0;

this.button1.text = "开始接收";

this.button1.click += new system.eventhandler(this.button1_click);

//

// groupbox3

//

this.groupbox3.controls.add(this.progressbar1);

this.groupbox3.controls.add(this.textbox8);

this.groupbox3.controls.add(this.textbox9);

this.groupbox3.controls.add(this.label8);

this.groupbox3.controls.add(this.label10);

this.groupbox3.controls.add(this.label11);

this.groupbox3.controls.add(this.label12);

this.groupbox3.controls.add(this.textbox10);

this.groupbox3.controls.add(this.textbox2);

this.groupbox3.controls.add(this.label2);

this.groupbox3.controls.add(this.textbox3);

this.groupbox3.controls.add(this.label5);

this.groupbox3.controls.add(this.label6);

this.groupbox3.location = new system.drawing.point(0, 72);

this.groupbox3.name = "groupbox3";

this.groupbox3.size = new system.drawing.size(408, 184);

this.groupbox3.tabindex = 4;

this.groupbox3.tabstop = false;

this.groupbox3.text = "状态信息";

//

// groupbox2

//

this.groupbox2.controls.add(this.textbox5);

this.groupbox2.controls.add(this.label4);

this.groupbox2.controls.add(this.textbox4);

this.groupbox2.controls.add(this.label3);

this.groupbox2.controls.add(this.textbox1);

this.groupbox2.controls.add(this.label1);

this.groupbox2.location = new system.drawing.point(0, 0);

this.groupbox2.name = "groupbox2";

this.groupbox2.size = new system.drawing.size(408, 72);

this.groupbox2.tabindex = 3;

this.groupbox2.tabstop = false;

this.groupbox2.text = "系统设置";

//

// textbox5

//

this.textbox5.location = new system.drawing.point(320, 40);

this.textbox5.name = "textbox5";

this.textbox5.size = new system.drawing.size(80, 21);

this.textbox5.tabindex = 3;

this.textbox5.text = "8888";

//

// label4

//

this.label4.location = new system.drawing.point(280, 48);

this.label4.name = "label4";

this.label4.size = new system.drawing.size(100, 16);

this.label4.tabindex = 2;

this.label4.text = "端口:";

//

// textbox4

//

this.textbox4.location = new system.drawing.point(104, 16);

this.textbox4.name = "textbox4";

this.textbox4.readonly = true;

this.textbox4.size = new system.drawing.size(160, 21);

this.textbox4.tabindex = 1;

this.textbox4.text = "";

//

// label3

//

this.label3.location = new system.drawing.point(16, 24);

this.label3.name = "label3";

this.label3.size = new system.drawing.size(100, 16);

this.label3.tabindex = 0;

this.label3.text = "本机ip地址:";

//

// textbox1

//

this.textbox1.location = new system.drawing.point(104, 40);

this.textbox1.name = "textbox1";

this.textbox1.size = new system.drawing.size(160, 21);

this.textbox1.tabindex = 1;

this.textbox1.text = "";

//

// label1

//

this.label1.location = new system.drawing.point(16, 48);

this.label1.name = "label1";

this.label1.size = new system.drawing.size(100, 16);

this.label1.tabindex = 0;

this.label1.text = "服务端ip地址:";

//

// label2

//

this.label2.location = new system.drawing.point(64, 24);

this.label2.name = "label2";

this.label2.size = new system.drawing.size(100, 16);

this.label2.tabindex = 0;

this.label2.text = "文件名:";

//

// textbox2

//

this.textbox2.location = new system.drawing.point(120, 16);

this.textbox2.name = "textbox2";

this.textbox2.readonly = true;

this.textbox2.size = new system.drawing.size(160, 21);

this.textbox2.tabindex = 1;

this.textbox2.text = "";

//

// textbox8

//

this.textbox8.location = new system.drawing.point(120, 64);

this.textbox8.name = "textbox8";

this.textbox8.readonly = true;

this.textbox8.size = new system.drawing.size(160, 21);

this.textbox8.tabindex = 5;

this.textbox8.text = "";

//

// textbox9

//

this.textbox9.location = new system.drawing.point(120, 88);

this.textbox9.name = "textbox9";

this.textbox9.readonly = true;

this.textbox9.size = new system.drawing.size(80, 21);

this.textbox9.tabindex = 4;

this.textbox9.text = "";

//

// label8

//

this.label8.location = new system.drawing.point(40, 72);

this.label8.name = "label8";

this.label8.size = new system.drawing.size(80, 16);

this.label8.tabindex = 8;

this.label8.text = "包的总数量:";

//

// label10

//

this.label10.location = new system.drawing.point(8, 96);

this.label10.name = "label10";

this.label10.size = new system.drawing.size(120, 16);

this.label10.tabindex = 9;

this.label10.text = "最后一个包的大小:";

//

// label11

//

this.label11.location = new system.drawing.point(200, 96);

this.label11.name = "label11";

this.label11.size = new system.drawing.size(96, 16);

this.label11.tabindex = 7;

this.label11.text = "(单位:字节)";

//

// label12

//

this.label12.location = new system.drawing.point(16, 120);

this.label12.name = "label12";

this.label12.size = new system.drawing.size(104, 16);

this.label12.tabindex = 6;

this.label12.text = "已接收包的数量:";

//

// textbox10

//

this.textbox10.location = new system.drawing.point(120, 112);

this.textbox10.name = "textbox10";

this.textbox10.readonly = true;

this.textbox10.size = new system.drawing.size(80, 21);

this.textbox10.tabindex = 3;

this.textbox10.text = "";

//

// progressbar1

//

this.progressbar1.location = new system.drawing.point(8, 152);

this.progressbar1.name = "progressbar1";

this.progressbar1.size = new system.drawing.size(392, 23);

this.progressbar1.step = 1;

this.progressbar1.tabindex = 10;

//

// textbox3

//

this.textbox3.location = new system.drawing.point(120, 40);

this.textbox3.name = "textbox3";

this.textbox3.readonly = true;

this.textbox3.size = new system.drawing.size(160, 21);

this.textbox3.tabindex = 5;

this.textbox3.text = "";

//

// label5

//

this.label5.location = new system.drawing.point(48, 48);

this.label5.name = "label5";

this.label5.size = new system.drawing.size(72, 16);

this.label5.tabindex = 8;

this.label5.text = "包的大小:";

//

// label6

//

this.label6.location = new system.drawing.point(288, 48);

this.label6.name = "label6";

this.label6.size = new system.drawing.size(96, 16);

this.label6.tabindex = 7;

this.label6.text = "(单位:字节)";

//

// ezonereceive

//

this.autoscalebasesize = new system.drawing.size(6, 14);

this.clientsize = new system.drawing.size(410, 311);

this.controls.add(this.groupbox3);

this.controls.add(this.groupbox2);

this.controls.add(this.groupbox1);

this.formborderstyle = system.windows.forms.formborderstyle.fixedsingle;

this.maximizebox = false;

this.name = "ezonereceive";

this.text = "亿众国际点对点文件传输软体接收端";

this.load += new system.eventhandler(this.ezonereceive_load);

this.groupbox1.resumelayout(false);

this.groupbox3.resumelayout(false);

this.groupbox2.resumelayout(false);

this.resumelayout(false);

}

#endregion

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[stathread]

static void main()

{

application.run(new ezonereceive());

}

private void button1_click(object sender, system.eventargs e)

{

//开启接收线程

thread tempthread=new thread(new threadstart(this.startreceive));

tempthread.start();

this.button1.enabled=false;

}

private void ezonereceive_load(object sender, system.eventargs e)

{

//获得本机的ip地址

this.textbox4.text=dns.gethostbyname(dns.gethostname()).addresslist[0].tostring();

}

private void startreceive()

{

//指向远程服务端节点

ipendpoint ipep=new ipendpoint(ipaddress.parse(this.textbox1.text),int.parse(this.textbox5.text));

//创建套接字

socket client=new socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp);

//连接到发送端

client.connect(ipep);

//获得[文件名]

string sendfilename=system.text.encoding.unicode.getstring(commonmodule.ezonemodule.receivevardata(client));

this.textbox2.text=sendfilename;

//获得[包的大小]

this.textbox3.text=system.text.encoding.unicode.getstring(commonmodule.ezonemodule.receivevardata(client));

//获得[包的总数量]

this.textbox8.text=system.text.encoding.unicode.getstring(commonmodule.ezonemodule.receivevardata(client));

this.progressbar1.maximum=int.parse(this.textbox8.text);

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

this.textbox9.text=system.text.encoding.unicode.getstring(commonmodule.ezonemodule.receivevardata(client));

//创建一个新文件

filestream myfilestream=new filestream(sendfilename,filemode.create,fileaccess.write);

//已发送包的个数

int sendedcount=0;

while(true)

{

byte[] data=commonmodule.ezonemodule.receivevardata(client);

if(data.length==0)

{

break;

}

else

{

sendedcount++;

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

myfilestream.write(data,0,data.length);

//显示已发送包的个数

this.textbox10.text=sendedcount.tostring();

//进度条值的显示

this.progressbar1.performstep();

}

}

this.progressbar1.value=this.progressbar1.maximum;

//关闭文件流

myfilestream.close();

//关闭套接字

client.close();

this.button1.enabled=true;

messagebox.show("文件接收完毕!");

}

}

}

//////////////////////////end-亿众国际接收端////////////////////////////////////

////////////////////////////begin-公共模块//////////////////////////////////////

using system;

using system.net;

using system.net.sockets;

using system.windows.forms;

namespace commonmodule

{

/// <summary>

/// class1 的摘要说明。

/// </summary>

public class ezonemodule

{

public ezonemodule()

{

//

// todo: 在此处添加构造函数逻辑

//

}

public static int senddata(socket s,byte[] data)

{

int total=0;

int size=data.length;

int dataleft=size;

int sent;

while(total<size)

{

sent=s.send(data,total,dataleft,socketflags.none);

total+=sent;

dataleft-=sent;

}

return total;

}

public static byte[] receivedata(socket s,int size)

{

int total=0;

int dataleft=size;

byte[] data=new byte[size];

int recv;

while(total<size)

{

recv=s.receive(data,total,dataleft,socketflags.none);

if(recv==0)

{

data=null;

break;

}

total+=recv;

dataleft-=recv;

}

return data;

}

public static int sendvardata(socket s,byte[] data)

{

int total=0;

int size=data.length;

int dataleft=size;

int sent;

byte[] datasize=new byte[4];

datasize=bitconverter.getbytes(size);

sent=s.send(datasize);

while(total<size)

{

sent=s.send(data,total,dataleft,socketflags.none);

total+=sent;

dataleft-=sent;

}

return total;

}

public static byte[] receivevardata(socket s)

{

int total=0;

int recv;

byte[] datasize=new byte[4];

recv=s.receive(datasize,0,4,socketflags.none);

int size=bitconverter.toint32(datasize,0);

int dataleft=size;

byte[] data=new byte[size];

while(total<size)

{

recv=s.receive(data,total,dataleft,socketflags.none);

if(recv==0)

{

data=null;

break;

}

total+=recv;

dataleft-=recv;

}

return data;

}

}

}

/////////////////////////////end-公共模块///////////////////////////////////////

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 亿众国际点对点文件传输程序-.NET教程,文件IO
分享到: 更多 (0)