您好,欢迎访问三七文档
c#winformsocket网络编程,点对点传输文件,socket文件传输,监听端口关键字:socket网络编程点对点传输文件文件传输监听端口服务器用来接收文件,不停的监听端口,有发送文件就马上开始接收文件服务端代码:C#代码1.usingSystem;2.usingSystem.Collections.Generic;3.usingSystem.ComponentModel;4.usingSystem.Data;5.usingSystem.Drawing;6.usingSystem.Text;7.usingSystem.Windows.Forms;8.9.10.usingSystem.Net;11.usingSystem.Threading;12.usingSystem.Net.Sockets;13.14.usingSystem.IO;15.16.namespaceTestSocketServerHSTF17.{18.publicpartialclassForm1:Form19.{20.publicForm1()21.{22.InitializeComponent();23.24.25.//不显示出dataGridView1的最后一行空白26.dataGridView1.AllowUserToAddRows=false;27.}28.29.30.#region定义变量31.32.33.#endregion34.35.36.37.#region进入窗体即启动服务38.39.privatevoidForm1_Load(objectsender,EventArgse)40.{41.//开启接收线程42.ThreadTempThread=newThread(newThreadStart(this.StartReceive));43.TempThread.Start();44.}45.46.47.#endregion48.49.50.51.#region功能函数52.53.privatevoidStartReceive()54.{55.//创建一个网络端点56.IPEndPointipep=newIPEndPoint(IPAddress.Any,int.Parse(2005));57.58.//MessageBox.Show(IPAddress.Any);59.60.//创建一个套接字61.Socketserver=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);62.63.//绑定套接字到端口64.server.Bind(ipep);65.66.//开始侦听(并堵塞该线程)67.server.Listen(10);68.69.//确认连接70.Socketclient=server.Accept();71.72.//获得客户端节点对象73.IPEndPointclientep=(IPEndPoint)client.RemoteEndPoint;74.75.76.77.//获得[文件名]78.stringSendFileName=System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));79.//MessageBox.Show(文件名+SendFileName);80.81.//获得[包的大小]82.stringbagSize=System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));83.//MessageBox.Show(包大小+bagSize);84.85.//获得[包的总数量]86.intbagCount=int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));87.//MessageBox.Show(包的总数量+bagCount);88.89.//获得[最后一个包的大小]90.stringbagLast=System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));91.//MessageBox.Show(最后一个包的大小+bagLast);92.93.//创建一个新文件94.FileStreamMyFileStream=newFileStream(SendFileName,FileMode.Create,FileAccess.Write);95.96.//已发送包的个数97.intSendedCount=0;98.99.while(true)100.{101.byte[]data=TransferFiles.ReceiveVarData(client);102.if(data.Length==0)103.{104.break;105.}106.else107.{108.SendedCount++;109.//将接收到的数据包写入到文件流对象110.MyFileStream.Write(data,0,data.Length);111.//显示已发送包的个数112.//MessageBox.Show(已发送包个数+SendedCount.ToString());113.}114.}115.116.//关闭文件流117.MyFileStream.Close();118.//关闭套接字119.client.Close();120.121.//填加到dgv里122.//文件大小,IP,已发送包的个数,文件名,包的总量,最后一个包的大小123.this.dataGridView1.Rows.Add(bagSize,clientep.Address,SendedCount,SendFileName,bagCount,bagLast);124.125.//MessageBox.Show(文件接收完毕!);126.127.}128.129.130.#endregion131.132.133.134.#region拦截Windows消息,关闭窗体时执行135.protectedoverridevoidWndProc(refMessagem)136.{137.constintWM_SYSCOMMAND=0x0112;138.constintSC_CLOSE=0xF060;139.if(m.Msg==WM_SYSCOMMAND&&(int)m.WParam==SC_CLOSE)140.{//捕捉关闭窗体消息141.//Userclickedclosebutton142.//this.WindowState=FormWindowState.Minimized;//把右上角红叉关闭按钮变最小化143.144.ServiceStop();145.}146.base.WndProc(refm);147.}148.#endregion149.150.151.#region停止服务152.153.//停止服务154.privatevoidServiceStop()155.{156.try157.{158.159.}160.catch{}161.162.try163.{164.165.}166.catch{}167.}168.169.#endregion170.171.}172.}客户端用来发送文件,选择文件后点发送按钮发送文件客户端代码:C#代码1.////////////////////////////////////////////////////////////////////////////////2.//title:点对点文件传输程序//3.////////////////////////////////////////////////////////////////////////////////4.5.//////////////////////////Begin-发送端//////////////////////////////////6.usingSystem;7.usingSystem.Drawing;8.usingSystem.Collections;9.usingSystem.ComponentModel;10.usingSystem.Windows.Forms;11.usingSystem.Data;12.usingSystem.IO;13.usingSystem.Net;14.usingSystem.Net.Sockets;15.usingSystem.Threading;16.17.namespace发送端18.{19.///summary20.///Form1的摘要说明。21.////summary22.publicclassForm1:System.Windows.Forms.Form23.{24.privateSystem.Windows.Forms.GroupBoxgroupBox1;25.privateSystem.Windows.Forms.OpenFileDialogopenFileDialog1;26.privateSystem.Windows.Forms.TextBoxtextBox1;27.privateSystem.Windows.Forms.Buttonbutton1;28.privateSystem.Windows.Forms.Labellabel1;29.privateSystem.Windows.Forms.TextBoxtextBox2;30.privateSystem.Windows.Forms.Labellabel2;31.privateSystem.Windows.Forms.TextBoxtextBox3;32.privateSystem.Windows.Forms.GroupBoxgroupBox2;33.privateSystem.Windows.Forms.Labellabel3;34.privateSystem.Windows.Forms.TextBoxtextBox4;35.privateSystem.Windows.Forms.Labellabel4;36.privateSystem.Windows.Forms.TextBoxtextBox5;37.privateSystem.Windows.Forms.GroupBoxgroupBox3;38.privateSystem.Windows.Forms.GroupBoxgroupBox4;39.privateSystem.Windows.Forms.Buttonbutton2;40.privateSystem.Windows.Forms.Labellabel5;41.privateSystem.Windows.Forms.TextBoxtextBox6;42.privateSystem.Windows.Forms.Labellabel6;43.privateSystem.Windows.Forms.Labellabel7;44.privateSystem.Windows.Forms.ProgressBarprogressBar1;45.privateSystem.Windows.Forms.TextBoxtextBox7;46.privateSystem.Windows.Forms.Labellabel8;47.privateSystem.Windows.Forms.Labellabel9;48.privateSystem.Windows.Forms.TextBoxtextBox8;49.privateSyst
本文标题:c# winform socket网络编程,点对点传输文件,socket文件传输,监听端口
链接地址:https://www.777doc.com/doc-4427906 .html