c# winform socket网络编程,点对点传输文件,socket文件传输,监听端口

http://www.cnblogs.com/vhtt/
服务器用来接收文件,不停的监听端口,有发送文件就马上开始接收文件 
服务端代码: 
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.   
  9.   
  10. using System.Net;  
  11. using System.Threading;  
  12. using System.Net.Sockets;  
  13.   
  14. using System.IO;  
  15.   
  16. namespace TestSocketServerHSTF  
  17. {  
  18.     public partial class Form1 : Form  
  19.     {  
  20.         public Form1()  
  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.         private void Form1_Load(object sender, EventArgs e)  
  40.         {  
  41.             //开启接收线程  
  42.             Thread TempThread = new Thread(new ThreadStart(this.StartReceive));  
  43.             TempThread.Start();  
  44.         }  
  45.  
  46.          
  47.         #endregion  
  48.  
  49.  
  50.  
  51.         #region 功能函数  
  52.   
  53.         private void StartReceive()  
  54.         {  
  55.             //创建一个网络端点  
  56.             IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse("2005"));  
  57.   
  58.             //MessageBox.Show(IPAddress.Any);  
  59.   
  60.             //创建一个套接字  
  61.             Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  
  62.   
  63.             //绑定套接字到端口  
  64.             server.Bind(ipep);  
  65.   
  66.             //开始侦听(并堵塞该线程)  
  67.             server.Listen(10);  
  68.   
  69.             //确认连接  
  70.             Socket client = server.Accept();  
  71.   
  72.             //获得客户端节点对象  
  73.             IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;  
  74.               
  75.   
  76.   
  77.             //获得[文件名]  
  78.             string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));  
  79.             //MessageBox.Show("文件名" + SendFileName);  
  80.   
  81.             //获得[包的大小]  
  82.             string bagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));  
  83.             //MessageBox.Show("包大小" + bagSize);  
  84.   
  85.             //获得[包的总数量]  
  86.             int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));  
  87.             //MessageBox.Show("包的总数量" + bagCount);  
  88.   
  89.             //获得[最后一个包的大小]  
  90.             string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));  
  91.             //MessageBox.Show("最后一个包的大小" + bagLast);  
  92.   
  93.             //创建一个新文件  
  94.             FileStream MyFileStream = new FileStream(SendFileName, FileMode.Create, FileAccess.Write);  
  95.   
  96.             //已发送包的个数  
  97.             int SendedCount = 0;  
  98.   
  99.             while (true)  
  100.             {  
  101.                 byte[] data = TransferFiles.ReceiveVarData(client);  
  102.                 if (data.Length == 0)  
  103.                 {  
  104.                     break;  
  105.                 }  
  106.                 else  
  107.                 {  
  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.         protected override void WndProc(ref   Message m)  
  136.         {  
  137.             const int WM_SYSCOMMAND = 0x0112;  
  138.             const int SC_CLOSE = 0xF060;  
  139.             if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)  
  140.             {//捕捉关闭窗体消息     
  141.                 //   User   clicked   close   button     
  142.                 //this.WindowState = FormWindowState.Minimized;//把右上角红叉关闭按钮变最小化  
  143.   
  144.                 ServiceStop();  
  145.             }  
  146.             base.WndProc(ref   m);  
  147.         }  
  148.         #endregion  
  149.  
  150.  
  151.         #region 停止服务  
  152.   
  153.         //停止服务  
  154.         private void ServiceStop()  
  155.         {  
  156.             try  
  157.             {  
  158.   
  159.             }  
  160.             catch { }  
  161.   
  162.             try  
  163.             {  
  164.   
  165.             }  
  166.             catch { }  
  167.         }  
  168.  
  169.         #endregion  
  170.   
  171.     }  
  172. }  


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


公共类,服务端客户端都需要用到 

C#代码 
  1. ////////////////////////////Begin-公共模块//////////////////////////////////////  
  2.   
  3.   
  4. using System;  
  5. using System.Collections.Generic;  
  6. using System.Text;  
  7.   
  8. using System.Net;  
  9. using System.Net.Sockets;  
  10. using System.Windows.Forms;  
  11.   
  12. namespace TestSocketServerHSTF  
  13. {  
  14.     class TransferFiles  
  15.     {  
  16.         public TransferFiles()  
  17.         {  
  18.             //  
  19.             // TODO: 在此处添加构造函数逻辑  
  20.             //  
  21.         }  
  22.   
  23.   
  24.   
  25.         public static int SendData(Socket s, byte[] data)  
  26.         {  
  27.             int total = 0;  
  28.             int size = data.Length;  
  29.             int dataleft = size;  
  30.             int sent;  
  31.   
  32.             while (total < size)  
  33.             {  
  34.                 sent = s.Send(data, total, dataleft, SocketFlags.None);  
  35.                 total += sent;  
  36.                 dataleft -= sent;  
  37.             }  
  38.   
  39.             return total;  
  40.         }  
  41.   
  42.         public static byte[] ReceiveData(Socket s, int size)  
  43.         {  
  44.             int total = 0;  
  45.             int dataleft = size;  
  46.             byte[] data = new byte[size];  
  47.             int recv;  
  48.             while (total < size)  
  49.             {  
  50.                 recv = s.Receive(data, total, dataleft, SocketFlags.None);  
  51.                 if (recv == 0)  
  52.                 {  
  53.                     data = null;  
  54.                     break;  
  55.                 }  
  56.   
  57.                 total += recv;  
  58.                 dataleft -= recv;  
  59.             }  
  60.             return data;  
  61.         }  
  62.   
  63.         public static int SendVarData(Socket s, byte[] data)  
  64.         {  
  65.             int total = 0;  
  66.             int size = data.Length;  
  67.             int dataleft = size;  
  68.             int sent;  
  69.             byte[] datasize = new byte[4];  
  70.             datasize = BitConverter.GetBytes(size);  
  71.             sent = s.Send(datasize);  
  72.   
  73.             while (total < size)  
  74.             {  
  75.                 sent = s.Send(data, total, dataleft, SocketFlags.None);  
  76.                 total += sent;  
  77.                 dataleft -= sent;  
  78.             }  
  79.   
  80.             return total;  
  81.         }  
  82.   
  83.         public static byte[] ReceiveVarData(Socket s)  
  84.         {  
  85.             int total = 0;  
  86.             int recv;  
  87.             byte[] datasize = new byte[4];  
  88.             recv = s.Receive(datasize, 0, 4, SocketFlags.None);  
  89.             int size = BitConverter.ToInt32(datasize, 0);  
  90.             int dataleft = size;  
  91.             byte[] data = new byte[size];  
  92.             while (total < size)  
  93.             {  
  94.                 recv = s.Receive(data, total, dataleft, SocketFlags.None);  
  95.                 if (recv == 0)  
  96.                 {  
  97.                     data = null;  
  98.                     break;  
  99.                 }  
  100.                 total += recv;  
  101.                 dataleft -= recv;  
  102.             }  
  103.             return data;  
  104.         }  
  105.     }  
  106. }  
  107.   
  108.   
  109.   
  110.   
  111. /////////////////////////////End-公共模块///////////////////////////////////////  

你可能感兴趣的:(WinForm)