




已阅读5页,还剩19页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
c# winform socket网络编程,点对点传输文件,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. namespaceTestSocketServerHSTF 17. 18. publicpartialclassForm1:Form 19. 20. publicForm1() 21. 22. InitializeComponent(); 23. 24. 25. /不显示出dataGridView1的最后一行空白 26. dataGridView1.AllowUserToAddRows=false; 27. 28. 29. 30. #region定义变量 31. 32. 33. #endregion 34. 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. #endregion 48. 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. bytedata=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. #endregion 131. 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. /Userclickedclosebutton 142. /this.WindowState=FormWindowState.Minimized;/把右上角红叉关闭按钮变最小化 143. 144. ServiceStop(); 145. 146. base.WndProc(refm); 147. 148. #endregion 149. 150. 151. #region停止服务 152. 153. /停止服务 154. privatevoidServiceStop() 155. 156. try157. 158. 159. 160. catch 161. 162. try163. 164. 165. 166. catch 167. 168. 169. #endregion 170. 171. 172. using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.Threading;using System.Net.Sockets;using System.IO;namespace TestSocketServerHSTFpublic partial class Form1 : Formpublic Form1()InitializeComponent();/不显示出dataGridView1的最后一行空白dataGridView1.AllowUserToAddRows = false;#region 定义变量#endregion#region 进入窗体即启动服务private void Form1_Load(object sender, EventArgs e)/开启接收线程Thread TempThread = new Thread(new ThreadStart(this.StartReceive);TempThread.Start();#endregion#region 功能函数private void StartReceive()/创建一个网络端点IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse(2005);/MessageBox.Show(IPAddress.Any);/创建一个套接字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;/获得文件名string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client);/MessageBox.Show(文件名 + SendFileName);/获得包的大小string bagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client);/MessageBox.Show(包大小 + bagSize);/获得包的总数量int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client);/MessageBox.Show(包的总数量 + bagCount);/获得最后一个包的大小string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client);/MessageBox.Show(最后一个包的大小 + bagLast);/创建一个新文件FileStream MyFileStream = new FileStream(SendFileName, FileMode.Create, FileAccess.Write);/已发送包的个数int SendedCount = 0;while (true)byte data = TransferFiles.ReceiveVarData(client);if (data.Length = 0)break;elseSendedCount+;/将接收到的数据包写入到文件流对象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消息,关闭窗体时执行protected override void WndProc(ref Message m)const int WM_SYSCOMMAND = 0x0112;const int SC_CLOSE = 0xF060;if (m.Msg = WM_SYSCOMMAND & (int)m.WParam = SC_CLOSE)/捕捉关闭窗体消息 / User clicked close button /this.WindowState = FormWindowState.Minimized;/把右上角红叉关闭按钮变最小化ServiceStop();base.WndProc(ref m);#endregion#region 停止服务/停止服务private void ServiceStop()trycatch trycatch #endregion客户端用来发送文件,选择文件后点发送按钮发送文件 客户端代码: 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. / 20. /Form1的摘要说明。 21. / 22. publicclassForm1:System.Windows.Forms.Form 23. 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. privateSystem.Windows.Forms.Labellabel10; 50. privateSystem.Windows.Forms.TextBoxtextBox9; 51. privateSystem.Windows.Forms.Labellabel11; 52. privateSystem.Windows.Forms.Labellabel12; 53. privateSystem.Windows.Forms.TextBoxtextBox10; 54. / 55. /必需的设计器变量。 56. / 57. privateSystem.ComponentModel.Containercomponents=null; 58. 59. publicForm1() 60. 61. / 62. /Windows窗体设计器支持所必需的 63. / 64. InitializeComponent(); 65. 66. / 67. /TODO:在InitializeComponent调用后添加任何构造函数代码 68. / 69. 70. 71. / 72. /清理所有正在使用的资源。 73. / 74. protectedoverridevoidDispose(booldisposing) 75. 76. if(disposing) 77. 78. if(components!=null) 79. 80. components.Dispose(); 81. 82. 83. base.Dispose(disposing); 84. 85. 86. #regionWindows窗体设计器生成的代码 87. / 88. /设计器支持所需的方法-不要使用代码编辑器修改 89. /此方法的内容。 90. / 91. privatevoidInitializeComponent() 92. 93. this.groupBox1=newSystem.Windows.Forms.GroupBox(); 94. this.textBox2=newSystem.Windows.Forms.TextBox(); 95. this.textBox3=newSystem.Windows.Forms.TextBox(); 96. this.label2=newSystem.Windows.Forms.Label(); 97. this.label1=newSystem.Windows.Forms.Label(); 98. this.button1=newSystem.Windows.Forms.Button(); 99. this.textBox1=newSystem.Windows.Forms.TextBox(); 100. this.label6=newSystem.Windows.Forms.Label(); 101. this.openFileDialog1=newSystem.Windows.Forms.OpenFileDialog(); 102. this.groupBox2=newSystem.Windows.Forms.GroupBox(); 103. this.textBox6=newSystem.Windows.Forms.TextBox(); 104. this.textBox5=newSystem.Windows.Forms.TextBox(); 105. this.label4=newSystem.Windows.Forms.Label(); 106. this.textBox4=newSystem.Windows.Forms.TextBox(); 107. this.label3=newSystem.Windows.Forms.Label(); 108. this.label5=newSystem.Windows.Forms.Label(); 109. this.label9=newSystem.Windows.Forms.Label(); 110. this.groupBox3=newSystem.Windows.Forms.GroupBox(); 111. this.textBox8=newSystem.Windows.Forms.TextBox(); 112. this.textBox9=newSystem.Windows.Forms.TextBox(); 113. this.textBox7=newSystem.Windows.Forms.TextBox(); 114. gressBar1=newSystem.Windows.Forms.ProgressBar(); 115. this.label7=newSystem.Windows.Forms.Label(); 116. this.label8=newSystem.Windows.Forms.Label(); 117. this.label10=newSystem.Windows.Forms.Label(); 118. this.label11=newSystem.Windows.Forms.Label(); 119. this.label12=newSystem.Windows.Forms.Label(); 120. this.textBox10=newSystem.Windows.Forms.TextBox(); 121. this.groupBox4=newSystem.Windows.Forms.GroupBox(); 122. this.button2=newSystem.Windows.Forms.Button(); 123. this.groupBox1.SuspendLayout(); 124. this.groupBox2.SuspendLayout(); 125. this.groupBox3.SuspendLayout(); 126. this.groupBox4.SuspendLayout(); 127. this.SuspendLayout(); 128. / 129. /groupBox1 130. / 131. this.groupBox1.Controls.Add(this.textBox2); 132. this.groupBox1.Controls.Add(this.textBox3); 133. this.groupBox1.Controls.Add(this.label2); 134. this.groupBox1.Controls.Add(this.label1); 135. this.groupBox1.Controls.Add(this.button1); 136. this.groupBox1.Controls.Add(this.textBox1); 137. this.groupBox1.Controls.Add(this.label6); 138. this.groupBox1.Location=newSystem.Drawing.Point(0,0); 139. this.groupBox1.Name=groupBox1; 140. this.groupBox1.Size=newSystem.Drawing.Size(416,96); 141. this.groupBox1.TabIndex=0; 142. this.groupBox1.TabStop=false; 143. this.groupBox1.Text=文件信息; 144. / 145. /textBox2 146. / 147. this.textBox2.Location=newSystem.Drawing.Point(80,40); 148. this.textBox2.Name=textBox2; 149. this.textBox2.ReadOnly=true; 150. this.textBox2.Size=newSystem.Drawing.Size(232,21); 151. this.textBox2.TabIndex=3; 152. / 153. /textBox3 154. / 155. this.textBox3.Location=newSystem.Drawing.Point(80,64); 156. this.textBox3.Name=textBox3; 157. this.textBox3.ReadOnly=true; 158. this.textBox3.Size=newSystem.Drawing.Size(136,21); 159. this.textBox3.TabIndex=3; 160. / 161. /label2 162. / 163. this.label2.Location=newSystem.Drawing.Point(8,72); 164. this.label2.Name=label2; 165. this.label2.Size=newSystem.Drawing.Size(100,16); 166. this.label2.TabIndex=4; 167. this.label2.Text=文件大小:; 168. / 169. /label1 170. / 171. this.label1.Location=newSystem.Drawing.Point(16,48); 172. this.label1.Name=label1; 173. this.label1.Size=newSystem.Drawing.Size(96,16); 174. this.label1.TabIndex=2; 175. this.label1.Text=文件名:; 176. / 177. /button1 178. / 179. this.button1.Location=newSystem.Drawing.Point(320,16); 180. this.button1.Name=button1; 181. this.button1.Size=newSystem.Dr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 个人协议用工范文
- 脑梗塞康复护理健康教育
- 2025年事业单位工勤技能-湖南-湖南地质勘查员三级(高级工)历年参考题库典型考点含答案解析
- 2025年事业单位工勤技能-湖北-湖北行政岗位工四级(中级工)历年参考题库含答案解析
- 2025年事业单位工勤技能-湖北-湖北检验员四级(中级工)历年参考题库典型考点含答案解析
- 2025年医药电商平台医药电商保险与合规监管报告
- 2025年事业单位工勤技能-湖北-湖北护理员五级(初级工)历年参考题库含答案解析
- 2025年事业单位工勤技能-湖北-湖北地图绘制员五级(初级工)历年参考题库典型考点含答案解析
- 2025-2030中国素颜霜市场需求状况及销售模式预测分析报告
- 2025年事业单位工勤技能-浙江-浙江药剂员一级(高级技师)历年参考题库含答案解析(5套)
- 蚊媒传染病的预防与控制
- 结构化学分子的对称性
- 大厦消防工程技术标
- 水中总氯的测定方法确认实验报告(HJ586)
- GB/T 1228-2006钢结构用高强度大六角头螺栓
- 第二章-基因工程的载体和工具酶课件
- 政府采购评审专家考试题库(含答案)
- 实验室新员工入职培训课件
- 动力柜技术协议
- 2023年青岛市城阳区工会系统招聘考试笔试题库及答案解析
- 高中生物第一课-(共24张)课件
评论
0/150
提交评论