技术文档 >ASP.NET >

C#文件传输

作者:佚名 2008-11-18 17:09:35 浏览/评论:2/0

服务器用来接收文件,不停的监听端口,有发送文件就马上开始接收文件
服务端代码:

C#代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.Threading;
  10. using System.Net.Sockets;
  11. using System.IO;
  12. namespace TestSocketServerHSTF
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. //不显示出dataGridView1的最后一行空白
  20. dataGridView1.AllowUserToAddRows = false;
  21. }
  22. #region 定义变量
  23. #endregion
  24. #region 进入窗体即启动服务
  25. private void Form1_Load(object sender, EventArgs e)
  26. {
  27. //开启接收线程
  28. Thread TempThread = new Thread(new ThreadStart(this.StartReceive));
  29. TempThread.Start();
  30. }
  31. #endregion
  32. #region 功能函数
  33. private void StartReceive()
  34. {
  35. //创建一个网络端点
  36. IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse("2005"));
  37. //MessageBox.Show(IPAddress.Any);
  38. //创建一个套接字
  39. Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  40. //绑定套接字到端口
  41. server.Bind(ipep);
  42. //开始侦听(并堵塞该线程)
  43. server.Listen(10);
  44. //确认连接
  45. Socket client = server.Accept();
  46. //获得客户端节点对象
  47. IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
  48. //获得[文件名]
  49. string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
  50. //MessageBox.Show("文件名" + SendFileName);
  51. //获得[包的大小]
  52. string bagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
  53. //MessageBox.Show("包大小" + bagSize);
  54. //获得[包的总数量]
  55. int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));
  56. //MessageBox.Show("包的总数量" + bagCount);
  57. //获得[最后一个包的大小]
  58. string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
  59. //MessageBox.Show("最后一个包的大小" + bagLast);
  60. //创建一个新文件
  61. FileStream MyFileStream = new FileStream(SendFileName, FileMode.Create, FileAccess.Write);
  62. //已发送包的个数
  63. int SendedCount = 0;
  64. while (true)
  65. {
  66. byte[] data = TransferFiles.ReceiveVarData(client);
  67. if (data.Length == 0)
  68. {
  69. break;
  70. }
  71. else
  72. {
  73. SendedCount++;
  74. //将接收到的数据包写入到文件流对象
  75. MyFileStream.Write(data, 0, data.Length);
  76. //显示已发送包的个数
  77. //MessageBox.Show("已发送包个数"+SendedCount.ToString());
  78. }
  79. }
  80. //关闭文件流
  81. MyFileStream.Close();
  82. //关闭套接字
  83. client.Close();
  84. //填加到dgv里
  85. //文件大小,IP,已发送包的个数,文件名,包的总量,最后一个包的大小
  86. this.dataGridView1.Rows.Add(bagSize, clientep.Address, SendedCount, SendFileName, bagCount, bagLast);
  87. //MessageBox.Show("文件接收完毕!");
  88. }
  89. #endregion
  90. #region 拦截Windows消息,关闭窗体时执行
  91. protected override void WndProc(ref Message m)
  92. {
  93. const int WM_SYSCOMMAND = 0x0112;
  94. const int SC_CLOSE = 0xF060;
  95. if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
  96. {//捕捉关闭窗体消息
  97. // User clicked close button
  98. //this.WindowState = FormWindowState.Minimized;//把右上角红叉关闭按钮变最小化
  99. ServiceStop();
  100. }
  101. base.WndProc(ref m);
  102. }
  103. #endregion
  104. #region 停止服务
  105. //停止服务
  106. private void ServiceStop()
  107. {
  108. try
  109. {
  110. }
  111. catch { }
  112. try
  113. {
  114. }
  115. catch { }
  116. }
  117. #endregion
  118. }
  119. }



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

C#代码 复制代码
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //title: 点对点文件传输程序 //
  3. ////////////////////////////////////////////////////////////////////////////////
  4. //////////////////////////Begin-发送端//////////////////////////////////
  5. using System;
  6. using System.Drawing;
  7. using System.Collections;
  8. using System.ComponentModel;
  9. using System.Windows.Forms;
  10. using System.Data;
  11. using System.IO;
  12. using System.Net;
  13. using System.Net.Sockets;
  14. using System.Threading;
  15. namespace 发送端
  16. {
  17. /// <summary>
  18. /// Form1 的摘要说明。
  19. /// </summary>
  20. public class Form1 : System.Windows.Forms.Form
  21. {
  22. private System.Windows.Forms.GroupBox groupBox1;
  23. private System.Windows.Forms.OpenFileDialog openFileDialog1;
  24. private System.Windows.Forms.TextBox textBox1;
  25. private System.Windows.Forms.Button button1;
  26. private System.Windows.Forms.Label label1;
  27. private System.Windows.Forms.TextBox textBox2;
  28. private System.Windows.Forms.Label label2;
  29. private System.Windows.Forms.TextBox textBox3;
  30. private System.Windows.Forms.GroupBox groupBox2;
  31. private System.Windows.Forms.Label label3;
  32. private System.Windows.Forms.TextBox textBox4;
  33. private System.Windows.Forms.Label label4;
  34. private System.Windows.Forms.TextBox textBox5;
  35. private System.Windows.Forms.GroupBox groupBox3;
  36. private System.Windows.Forms.GroupBox groupBox4;
  37. private System.Windows.Forms.Button button2;
  38. private System.Windows.Forms.Label label5;
  39. private System.Windows.Forms.TextBox textBox6;
  40. private System.Windows.Forms.Label label6;
  41. private System.Windows.Forms.Label label7;
  42. private System.Windows.Forms.ProgressBar progressBar1;
  43. private System.Windows.Forms.TextBox textBox7;
  44. private System.Windows.Forms.Label label8;
  45. private System.Windows.Forms.Label label9;
  46. private System.Windows.Forms.TextBox textBox8;
  47. private System.Windows.Forms.Label label10;
  48. private System.Windows.Forms.TextBox textBox9;
  49. private System.Windows.Forms.Label label11;
  50. private System.Windows.Forms.Label label12;
  51. private System.Windows.Forms.TextBox textBox10;
  52. /// <summary>
  53. /// 必需的设计器变量。
  54. /// </summary>
  55. private System.ComponentModel.Container components = null;
  56. public Form1()
  57. {
  58. //
  59. // Windows 窗体设计器支持所必需的
  60. //
  61. InitializeComponent();
  62. //
  63. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  64. //
  65. }
  66. /// <summary>
  67. /// 清理所有正在使用的资源。
  68. /// </summary>
  69. protected override void Dispose( bool disposing )
  70. {
  71. if( disposing )
  72. {
  73. if (components != null)
  74. {
  75. components.Dispose();
  76. }
  77. }
  78. base.Dispose( disposing );
  79. }
  80. #region Windows 窗体设计器生成的代码
  81. /// <summary>
  82. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  83. /// 此方法的内容。
  84. /// </summary>
  85. private void InitializeComponent()
  86. {
  87. this.groupBox1 = new System.Windows.Forms.GroupBox();
  88. this.textBox2 = new System.Windows.Forms.TextBox();
  89. this.textBox3 = new System.Windows.Forms.TextBox();
  90. this.label2 = new System.Windows.Forms.Label();
  91. this.label1 = new System.Windows.Forms.Label();
  92. this.button1 = new System.Windows.Forms.Button();
  93. this.textBox1 = new System.Windows.Forms.TextBox();
  94. this.label6 = new System.Windows.Forms.Label();
  95. this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  96. this.groupBox2 = new System.Windows.Forms.GroupBox();
  97. this.textBox6 = new System.Windows.Forms.TextBox();
  98. this.textBox5 = new System.Windows.Forms.TextBox();
  99. this.label4 = new System.Windows.Forms.Label();
  100. this.textBox4 = new System.Windows.Forms.TextBox();
  101. this.label3 = new System.Windows.Forms.Label();
  102. this.label5 = new System.Windows.Forms.Label();
  103. this.label9 = new System.Windows.Forms.Label();
  104. this.groupBox3 = new System.Windows.Forms.GroupBox();
  105. this.textBox8 = new System.Windows.Forms.TextBox();
  106. this.textBox9 = new System.Windows.Forms.TextBox();
  107. this.textBox7 = new System.Windows.Forms.TextBox();
  108. this.progressBar1 = new System.Windows.Forms.ProgressBar();
  109. this.label7 = new System.Windows.Forms.Label();
  110. this.label8 = new System.Windows.Forms.Label();
  111. this.label10 = new System.Windows.Forms.Label();
  112. this.label11 = new System.Windows.Forms.Label();
  113. this.label12 = new System.Windows.Forms.Label();
  114. this.textBox10 = new System.Windows.Forms.TextBox();
  115. this.groupBox4 = new System.Windows.Forms.GroupBox();
  116. this.button2 = new System.Windows.Forms.Button();
  117. this.groupBox1.SuspendLayout();
  118. this.groupBox2.SuspendLayout();
  119. this.groupBox3.SuspendLayout();
  120. this.groupBox4.SuspendLayout();
  121. this.SuspendLayout();
  122. //
  123. // groupBox1
  124. //
  125. this.groupBox1.Controls.Add(this.textBox2);
  126. this.groupBox1.Controls.Add(this.textBox3);
  127. this.groupBox1.Controls.Add(this.label2);
  128. this.groupBox1.Controls.Add(this.label1);
  129. this.groupBox1.Controls.Add(this.button1);
  130. this.groupBox1.Controls.Add(this.textBox1);
  131. this.groupBox1.Controls.Add(this.label6);
  132. this.groupBox1.Location = new System.Drawing.Point(0, 0);
  133. this.groupBox1.Name = "groupBox1";
  134. this.groupBox1.Size = new System.Drawing.Size(416, 96);
  135. this.groupBox1.TabIndex = 0;
  136. this.groupBox1.TabStop = false;
  137. this.groupBox1.Text = "文件信息";
  138. //
  139. // textBox2
  140. //
  141. this.textBox2.Location = new System.Drawing.Point(80, 40);
  142. this.textBox2.Name = "textBox2";
  143. this.textBox2.ReadOnly = true;
  144. this.textBox2.Size = new System.Drawing.Size(232, 21);
  145. this.textBox2.TabIndex = 3;
  146. //
  147. // textBox3
  148. //
  149. this.textBox3.Location = new System.Drawing.Point(80, 64);
  150. this.textBox3.Name = "textBox3";
  151. this.textBox3.ReadOnly = true;
  152. this.textBox3.Size = new System.Drawing.Size(136, 21);
  153. this.textBox3.TabIndex = 3;
  154. //
  155. // label2
  156. //
  157. this.label2.Location = new System.Drawing.Point(8, 72);
  158. this.label2.Name = "label2";
  159. this.label2.Size = new System.Drawing.Size(100, 16);
  160. this.label2.TabIndex = 4;
  161. this.label2.Text = "文件大小:";
  162. //
  163. // label1
  164. //
  165. this.label1.Location = new System.Drawing.Point(16, 48);
  166. this.label1.Name = "label1";
  167. this.label1.Size = new System.Drawing.Size(96, 16);
  168. this.label1.TabIndex = 2;
  169. this.label1.Text = "文件名:";
  170. //
  171. // button1
  172. //
  173. this.button1.Location = new System.Drawing.Point(320, 16);
  174. this.button1.Name = "button1";
  175. this.button1.Size = new System.Drawing.Size(88, 23);
  176. this.button1.TabIndex = 1;
  177. this.button1.Text = "浏览";
  178. this.button1.Click += new System.EventHandler(this.button1_Click);
  179. //
  180. // textBox1
  181. //
  182. this.textBox1.Location = new System.Drawing.Point(8, 16);
  183. this.textBox1.Name = "textBox1";
  184. this.textBox1.ReadOnly = true;
  185. this.textBox1.Size = new System.Drawing.Size(304, 21);
  186. this.textBox1.TabIndex = 0;
  187. //
  188. // label6
  189. //
  190. this.label6.Location = new System.Drawing.Point(224, 72);
  191. this.label6.Name = "label6";
  192. this.label6.Size = new System.Drawing.Size(96, 16);
  193. this.label6.TabIndex = 2;
  194. this.label6.Text = "(单位:字节)";
  195. //
  196. // openFileDialog1
  197. //
  198. this.openFileDialog1.Filter = "所有文件|*.*";
  199. //
  200. // groupBox2
  201. //
  202. this.groupBox2.Controls.Add(this.textBox6);
  203. this.groupBox2.Controls.Add(this.textBox5);
  204. this.groupBox2.Controls.Add(this.label4);
  205. this.groupBox2.Controls.Add(this.textBox4);
  206. this.groupBox2.Controls.Add(this.label3);
  207. this.groupBox2.Controls.Add(this.label5);
  208. this.groupBox2.Controls.Add(this.label9);
  209. this.groupBox2.Location = new System.Drawing.Point(0, 96);
  210. this.groupBox2.Name = "groupBox2";
  211. this.groupBox2.Size = new System.Drawing.Size(416, 72);
  212. this.groupBox2.TabIndex = 1;
  213. this.groupBox2.TabStop = false;
  214. this.groupBox2.Text = "系统设置";
  215. //
  216. // textBox6
  217. //
  218. this.textBox6.Location = new System.Drawing.Point(96, 40);
  219. this.textBox6.Name = "textBox6";
  220. this.textBox6.Size = new System.Drawing.Size(72, 21);
  221. this.textBox6.TabIndex = 3;
  222. this.textBox6.Text = "50000";
  223. //
  224. // textBox5
  225. //
  226. this.textBox5.Location = new System.Drawing.Point(320, 16);
  227. this.textBox5.Name = "textBox5";
  228. this.textBox5.Size = new System.Drawing.Size(80, 21);
  229. this.textBox5.TabIndex = 3;
  230. this.textBox5.Text = "2005";
  231. //
  232. // label4
  233. //
  234. this.label4.Location = new System.Drawing.Point(256, 24);
  235. this.label4.Name = "label4";
  236. this.label4.Size = new System.Drawing.Size(100, 16);
  237. this.label4.TabIndex = 2;
  238. this.label4.Text = "传输端口:";
  239. //
  240. // textBox4
  241. //
  242. this.textBox4.Location = new System.Drawing.Point(96, 16);
  243. this.textBox4.Name = "textBox4";
  244. this.textBox4.ReadOnly = true;
  245. this.textBox4.Size = new System.Drawing.Size(144, 21);
  246. this.textBox4.TabIndex = 1;
  247. //
  248. // label3
  249. //
  250. this.label3.Location = new System.Drawing.Point(16, 24);
  251. this.label3.Name = "label3";
  252. this.label3.Size = new System.Drawing.Size(100, 16);
  253. this.label3.TabIndex = 0;
  254. this.label3.Text = "本机IP地址:";
  255. //
  256. // label5
  257. //
  258. this.label5.Location = new System.Drawing.Point(24, 48);
  259. this.label5.Name = "label5";
  260. this.label5.Size = new System.Drawing.Size(88, 16);
  261. this.label5.TabIndex = 2;
  262. this.label5.Text = "包的大小:";
  263. //
  264. // label9
  265. //
  266. this.label9.Location = new System.Drawing.Point(176, 48);
  267. this.label9.Name = "label9";
  268. this.label9.Size = new System.Drawing.Size(224, 16);
  269. this.label9.TabIndex = 2;
  270. this.label9.Text = "(范围:10000 - 60000 单位:字节)";
  271. //
  272. // groupBox3
  273. //
  274. this.groupBox3.Controls.Add(this.textBox8);
  275. this.groupBox3.Controls.Add(this.textBox9);
  276. this.groupBox3.Controls.Add(this.textBox7);
  277. this.groupBox3.Controls.Add(this.progressBar1);
  278. this.groupBox3.Controls.Add(this.label7);
  279. this.groupBox3.Controls.Add(this.label8);
  280. this.groupBox3.Controls.Add(this.label10);
  281. this.groupBox3.Controls.Add(this.label11);
  282. this.groupBox3.Controls.Add(this.label12);
  283. this.groupBox3.Controls.Add(this.textBox10);
  284. this.groupBox3.Location = new System.Drawing.Point(0, 168);
  285. this.groupBox3.Name = "groupBox3";
  286. this.groupBox3.Size = new System.Drawing.Size(416, 168);
  287. this.groupBox3.TabIndex = 2;
  288. this.groupBox3.TabStop = false;
  289. this.groupBox3.Text = "状态信息";
  290. //
  291. // textBox8
  292. //
  293. this.textBox8.Location = new System.Drawing.Point(120, 40);
  294. this.textBox8.Name = "textBox8";
  295. this.textBox8.ReadOnly = true;
  296. this.textBox8.Size = new System.Drawing.Size(160, 21);
  297. this.textBox8.TabIndex = 1;
  298. //
  299. // textBox9
  300. //
  301. this.textBox9.Location = new System.Drawing.Point(120, 64);
  302. this.textBox9.Name = "textBox9";
  303. this.textBox9.ReadOnly = true;
  304. this.textBox9.Size = new System.Drawing.Size(80, 21);
  305. this.textBox9.TabIndex = 1;
  306. //
  307. // textBox7
  308. //
  309. this.textBox7.Location = new System.Drawing.Point(120, 16);
  310. this.textBox7.Name = "textBox7";
  311. this.textBox7.ReadOnly = true;
  312. this.textBox7.Size = new System.Drawing.Size(160, 21);
  313. this.textBox7.TabIndex = 1;
  314. //
  315. // progressBar1
  316. //
  317. this.progressBar1.Location = new System.Drawing.Point(8, 136);
  318. this.progressBar1.Name = "progressBar1";
  319. this.progressBar1.Size = new System.Drawing.Size(400, 23);
  320. this.progressBar1.Step = 1;
  321. this.progressBar1.TabIndex = 3;
  322. //
  323. // label7
  324. //
  325. this.label7.Location = new System.Drawing.Point(32, 24);
  326. this.label7.Name = "label7";
  327. this.label7.Size = new System.Drawing.Size(96, 16);
  328. this.label7.TabIndex = 2;
  329. this.label7.Text = "接收端IP地址:";
  330. //
  331. // label8
  332. //
  333. this.label8.Location = new System.Drawing.Point(40, 48);
  334. this.label8.Name = "label8";
  335. this.label8.Size = new System.Drawing.Size(80, 16);
  336. this.label8.TabIndex = 2;
  337. this.label8.Text = "包的总数量:";
  338. //
  339. // label10
  340. //
  341. this.label10.Location = new System.Drawing.Point(8, 72);
  342. this.label10.Name = "label10";
  343. this.label10.Size = new System.Drawing.Size(120, 16);
  344. this.label10.TabIndex = 2;
  345. this.label10.Text = "最后一个包的大小:";
  346. //
  347. // label11
  348. //
  349. this.label11.Location = new System.Drawing.Point(200, 72);
  350. this.label11.Name = "label11";
  351. this.label11.Size = new System.Drawing.Size(96, 16);
  352. this.label11.TabIndex = 2;
  353. this.label11.Text = "(单位:字节)";
  354. //
  355. // label12
  356. //
  357. this.label12.Location = new System.Drawing.Point(16, 96);
  358. this.label12.Name = "label12";
  359. this.label12.Size = new System.Drawing.Size(104, 16);
  360. this.label12.TabIndex = 2;
  361. this.label12.Text = "已发送包的数量:";
  362. //
  363. // textBox10
  364. //
  365. this.textBox10.Location = new System.Drawing.Point(120, 88);
  366. this.textBox10.Name = "textBox10";
  367. this.textBox10.ReadOnly = true;
  368. this.textBox10.Size = new System.Drawing.Size(80, 21);
  369. this.textBox10.TabIndex = 1;
  370. //
  371. // groupBox4
  372. //
  373. this.groupBox4.Controls.Add(this.button2);
  374. this.groupBox4.Location = new System.Drawing.Point(0, 336);
  375. this.groupBox4.Name = "groupBox4";
  376. this.groupBox4.Size = new System.Drawing.Size(416, 48);
  377. this.groupBox4.TabIndex = 3;
  378. this.groupBox4.TabStop = false;
  379. this.groupBox4.Text = "系统控制";
  380. //
  381. // button2
  382. //
  383. this.button2.Location = new System.Drawing.Point(16, 16);
  384. this.button2.Name = "button2";
  385. this.button2.Size = new System.Drawing.Size(75, 23);
  386. this.button2.TabIndex = 0;
  387. this.button2.Text = "开始发送";
  388. this.button2.Click += new System.EventHandler(this.button2_Click);
  389. //
  390. // Form1
  391. //
  392. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  393. this.ClientSize = new System.Drawing.Size(416, 389);
  394. this.Controls.Add(this.groupBox4);
  395. this.Controls.Add(this.groupBox3);
  396. this.Controls.Add(this.groupBox2);
  397. this.Controls.Add(this.groupBox1);
  398. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  399. this.MaximizeBox = false;
  400. this.Name = "Form1";
  401. this.Text = "点对点文件传输软体发送端";
  402. this.Load += new System.EventHandler(this.EzoneSend_Load);
  403. this.groupBox1.ResumeLayout(false);
  404. this.groupBox1.PerformLayout();
  405. this.groupBox2.ResumeLayout(false);
  406. this.groupBox2.PerformLayout();
  407. this.groupBox3.ResumeLayout(false);
  408. this.groupBox3.PerformLayout();
  409. this.groupBox4.ResumeLayout(false);
  410. this.ResumeLayout(false);
  411. }
  412. #endregion
  413. /// <summary>
  414. /// 应用程序的主入口点。
  415. /// </summary>
  416. [STAThread]
  417. static void Main()
  418. {
  419. Application.Run(new Form1());
  420. }
  421. private void button1_Click(object sender, System.EventArgs e)
  422. {
  423. //选择要进行传输的文件
  424. if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
  425. {
  426. FileInfo EzoneFile=new FileInfo(this.openFileDialog1.FileName);
  427. this.textBox1.Text=EzoneFile.FullName;
  428. this.textBox2.Text=EzoneFile.Name;
  429. this.textBox3.Text=EzoneFile.Length.ToString();
  430. }
  431. }
  432. private void StartSend()
  433. {
  434. //创建一个文件对象
  435. FileInfo EzoneFile=new FileInfo(this.textBox1.Text);
  436. //打开文件流
  437. FileStream EzoneStream=EzoneFile.OpenRead();
  438. //包的大小
  439. int PacketSize=int.Parse(this.textBox6.Text);
  440. //包的数量
  441. int PacketCount=(int)(EzoneStream.Length/((long)PacketSize));
  442. this.textBox8.Text=PacketCount.ToString();
  443. this.progressBar1.Maximum=PacketCount;
  444. //最后一个包的大小
  445. int LastDataPacket=(int)(EzoneStream.Length-((long)(PacketSize*PacketCount)));
  446. this.textBox9.Text=LastDataPacket.ToString();
  447. ////创建一个网络端点
  448. //IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.136"), int.Parse(this.textBox5.Text));
  449. ////MessageBox.Show(IPAddress.Any);
  450. ////创建一个套接字
  451. //Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  452. //MessageBox.Show(server.ToString());
  453. //指向远程服务端节点
  454. IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.136"), int.Parse(this.textBox5.Text));
  455. //创建套接字
  456. Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  457. //连接到发送端
  458. client.Connect(ipep);
  459. ////绑定套接字到端口
  460. //client.Bind(ipep);
  461. //MessageBox.Show(ipep.ToString());
  462. ////开始侦听(并堵塞该线程)
  463. //server.Listen(10);
  464. //确认连接
  465. //Socket client = server.Accept();
  466. //MessageBox.Show(client.ToString());
  467. //获得客户端节点对象
  468. IPEndPoint clientep=(IPEndPoint)client.RemoteEndPoint;
  469. //获得客户端的IP地址
  470. //this.textBox7.Text=clientep.Address.ToString();
  471. //发送[文件名]到客户端
  472. CommonModule.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(EzoneFile.Name));
  473. //发送[包的大小]到客户端
  474. CommonModule.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(PacketSize.ToString()));
  475. //发送[包的总数量]到客户端
  476. CommonModule.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(PacketCount.ToString()));
  477. //发送[最后一个包的大小]到客户端
  478. CommonModule.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(LastDataPacket.ToString()));
  479. //数据包
  480. byte[] data=new byte[PacketSize];
  481. //开始循环发送数据包
  482. for(int i=0;i<PacketCount;i++)
  483. {
  484. //从文件流读取数据并填充数据包
  485. EzoneStream.Read(data,0,data.Length);
  486. //发送数据包
  487. CommonModule.EzoneModule.SendVarData(client,data);
  488. //显示发送数据包的个数
  489. this.textBox10.Text=((int)(i+1)).ToString();
  490. //进度条值的显示
  491. this.progressBar1.PerformStep();
  492. }
  493. //如果还有多余的数据包,则应该发送完毕!
  494. if(LastDataPacket!=0)
  495. {
  496. data=new byte[LastDataPacket];
  497. EzoneStream.Read(data,0,data.Length);
  498. CommonModule.EzoneModule.SendVarData(client,data);
  499. this.progressBar1.Value=this.progressBar1.Maximum;
  500. }
  501. //关闭套接字
  502. client.Close();
  503. //server.Close();
  504. //关闭文件流
  505. EzoneStream.Close();
  506. this.button2.Enabled=true;
  507. MessageBox.Show("文件传输完毕!");
  508. }
  509. private void button2_Click(object sender, System.EventArgs e)
  510. {
  511. //开启文件传输子线程
  512. Thread TempThread=new Thread(new ThreadStart(this.StartSend));
  513. TempThread.Start();
  514. this.button2.Enabled=false;
  515. }
  516. private void EzoneSend_Load(object sender, System.EventArgs e)
  517. {
  518. //获得本机的IP地址
  519. this.textBox4.Text=Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
  520. }
  521. }
  522. }
//////////////////////////////////////////////////////////////////////////////////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 Form1 : 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 Form1()		{			//			// 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.textBox2 = new System.Windows.Forms.TextBox();			this.textBox3 = new System.Windows.Forms.TextBox();			this.label2 = new System.Windows.Forms.Label();			this.label1 = new System.Windows.Forms.Label();			this.button1 = new System.Windows.Forms.Button();			this.textBox1 = new System.Windows.Forms.TextBox();			this.label6 = new System.Windows.Forms.Label();			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();			this.groupBox2 = new System.Windows.Forms.GroupBox();			this.textBox6 = new System.Windows.Forms.TextBox();			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.label5 = new System.Windows.Forms.Label();			this.label9 = new System.Windows.Forms.Label();			this.groupBox3 = new System.Windows.Forms.GroupBox();			this.textBox8 = new System.Windows.Forms.TextBox();			this.textBox9 = new System.Windows.Forms.TextBox();			this.textBox7 = new System.Windows.Forms.TextBox();			this.progressBar1 = new System.Windows.Forms.ProgressBar();			this.label7 = new System.Windows.Forms.Label();			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.groupBox4 = new System.Windows.Forms.GroupBox();			this.button2 = new System.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 = 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 = "文件信息";			// 			// 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;			// 			// 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;			// 			// 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 = "文件大小:";			// 			// 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 = "文件名:";			// 			// 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);			// 			// 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;			// 			// 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 = "(单位:字节)";			// 			// openFileDialog1			// 			this.openFileDialog1.Filter = "所有文件|*.*";			// 			// 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 = "系统设置";			// 			// 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";			// 			// 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 = "2005";			// 			// 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 = "传输端口:";			// 			// 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;			// 			// 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地址:";			// 			// 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 = "包的大小:";			// 			// 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 单位:字节)";			// 			// 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 = "状态信息";			// 			// 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;			// 			// 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;			// 			// 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;			// 			// 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;			// 			// 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地址:";			// 			// 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 = "包的总数量:";			// 			// 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 = "最后一个包的大小:";			// 			// 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;			// 			// 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.Size = new System.Drawing.Size(75, 23);			this.button2.TabIndex = 0;			this.button2.Text = "开始发送";			this.button2.Click += new System.EventHandler(this.button2_Click);			// 			// Form1			// 			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 = "Form1";			this.Text = "点对点文件传输软体发送端";			this.Load += new System.EventHandler(this.EzoneSend_Load);			this.groupBox1.ResumeLayout(false);			this.groupBox1.PerformLayout();			this.groupBox2.ResumeLayout(false);			this.groupBox2.PerformLayout();			this.groupBox3.ResumeLayout(false);			this.groupBox3.PerformLayout();			this.groupBox4.ResumeLayout(false);			this.ResumeLayout(false);		}		#endregion		/// <summary>		/// 应用程序的主入口点。		/// </summary>		[STAThread]		static void Main() 		{			Application.Run(new Form1());		}		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.Parse("192.168.1.136"), int.Parse(this.textBox5.Text));			////MessageBox.Show(IPAddress.Any);			////创建一个套接字			//Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);			//MessageBox.Show(server.ToString());			//指向远程服务端节点			IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.136"), int.Parse(this.textBox5.Text));			//创建套接字			Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);			//连接到发送端			client.Connect(ipep);			////绑定套接字到端口			//client.Bind(ipep);			//MessageBox.Show(ipep.ToString());			////开始侦听(并堵塞该线程)			//server.Listen(10);			//确认连接			//Socket client = server.Accept();			//MessageBox.Show(client.ToString());									//获得客户端节点对象			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.E