fserver.cs
上传用户:yan_wy
上传日期:2007-06-09
资源大小:112k
文件大小:5k
源码类别:

ICQ/即时通讯

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.IO;
  9. using System.Text;
  10. using System.Threading ;
  11. namespace qq
  12. {
  13. /// <summary>
  14. /// fserver 的摘要说明。
  15. /// </summary>
  16. public class fserver : System.Windows.Forms.Form
  17. {
  18. int jieshou;
  19. Form1 form;
  20. string id;
  21. string path;
  22. Thread outputThread ;
  23. private TcpListener listener;
  24. private System.Windows.Forms.Label label1;
  25. private System.Windows.Forms.Button button1;
  26. Socket connection;
  27. System.Net.Sockets .NetworkStream socketstream;
  28. BinaryWriter writer;
  29. BinaryReader reader;
  30. private System.Windows.Forms.OpenFileDialog openFileDialog1;
  31. private System.Windows.Forms.Button button2;
  32. /// <summary>
  33. /// 必需的设计器变量。
  34. /// </summary>
  35. private System.ComponentModel.Container components = null;
  36. public fserver(Form1 f,string a)
  37. {
  38. //
  39. // Windows 窗体设计器支持所必需的
  40. //
  41. form=f;
  42. this.id=a;
  43. InitializeComponent();
  44. this.jieshou=0;
  45. //
  46. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  47. //
  48. }
  49. /// <summary>
  50. /// 清理所有正在使用的资源。
  51. /// </summary>
  52. protected override void Dispose( bool disposing )
  53. {
  54. if( disposing )
  55. {
  56. if(components != null)
  57. {
  58. components.Dispose();
  59. }
  60. }
  61. base.Dispose( disposing );
  62. }
  63. #region Windows 窗体设计器生成的代码
  64. /// <summary>
  65. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  66. /// 此方法的内容。
  67. /// </summary>
  68. private void InitializeComponent()
  69. {
  70. this.label1 = new System.Windows.Forms.Label();
  71. this.button1 = new System.Windows.Forms.Button();
  72. this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  73. this.button2 = new System.Windows.Forms.Button();
  74. this.SuspendLayout();
  75. // 
  76. // label1
  77. // 
  78. this.label1.Location = new System.Drawing.Point(64, 32);
  79. this.label1.Name = "label1";
  80. this.label1.Size = new System.Drawing.Size(120, 32);
  81. this.label1.TabIndex = 0;
  82. this.label1.Text = "发送文件";
  83. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  84. // 
  85. // button1
  86. // 
  87. this.button1.Location = new System.Drawing.Point(144, 112);
  88. this.button1.Name = "button1";
  89. this.button1.Size = new System.Drawing.Size(56, 48);
  90. this.button1.TabIndex = 1;
  91. this.button1.Text = "发送";
  92. this.button1.Click += new System.EventHandler(this.button1_Click);
  93. // 
  94. // button2
  95. // 
  96. this.button2.Location = new System.Drawing.Point(32, 112);
  97. this.button2.Name = "button2";
  98. this.button2.Size = new System.Drawing.Size(64, 48);
  99. this.button2.TabIndex = 2;
  100. this.button2.Text = "取消";
  101. this.button2.Click += new System.EventHandler(this.button2_Click);
  102. // 
  103. // fserver
  104. // 
  105. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  106. this.ClientSize = new System.Drawing.Size(264, 213);
  107. this.Controls.Add(this.button2);
  108. this.Controls.Add(this.button1);
  109. this.Controls.Add(this.label1);
  110. this.Name = "fserver";
  111. this.Text = "fserver";
  112. this.ResumeLayout(false);
  113. }
  114. #endregion
  115. public void sendfile()
  116. {
  117. this.label1.Text="等待对方。。。。";
  118. form.use.sendmessage(form.userid);
  119. form.use.sendmessage(id);
  120. form.use.sendmessage("发送文件");
  121. connection=listener.AcceptSocket();
  122. this.label1.Text="正在发送。。。。";
  123. socketstream=new NetworkStream(connection);
  124. writer=new BinaryWriter (socketstream);
  125. reader=new BinaryReader(socketstream);
  126. try 
  127. {
  128. byte[] b = new byte[1024];
  129.     System.IO.FileInfo fle=new FileInfo(path);
  130.     
  131. FileStream fs= File.Open(path, FileMode.Open);
  132. writer.Write(fs.Length.ToString());
  133. writer.Write(fle.Name);
  134. while (fs.Read(b,0,b.Length) > 0) 
  135. {
  136. writer.Write(b);
  137. }
  138. b = System.Text.Encoding.ASCII.GetBytes("传输结束") ;
  139. writer.Write(b);
  140. this.label1.Text="发送结束!!!!";
  141. writer.Close();
  142. reader.Close();
  143. fs.Close();
  144. connection.Close();
  145. listener.Stop();
  146. catch (Exception e) 
  147. {
  148. MessageBox.Show(e.ToString());
  149. }
  150. }
  151. public void setup()
  152. {
  153. // string path;
  154. if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
  155. {
  156. path = this.openFileDialog1.FileName;
  157. }
  158. try
  159. {
  160. IPAddress ipAddress = Dns.Resolve(this.form.ip).AddressList[0];
  161. this.listener=new TcpListener(ipAddress,5002);
  162. listener.Start();
  163. outputThread =new Thread (new ThreadStart (sendfile));
  164. outputThread.Start ();
  165. }
  166. catch(Exception ee)
  167. {
  168. MessageBox.Show(ee.ToString());
  169. }
  170. }
  171. private void button1_Click(object sender, System.EventArgs e)
  172. {
  173. this.jieshou=1;
  174. this.setup();
  175. }
  176. private void button2_Click(object sender, System.EventArgs e)
  177. {
  178. if(this.jieshou==1)
  179. {
  180. this.outputThread.Abort();
  181. listener.Stop();
  182. }
  183. this.Close();
  184. }
  185. }
  186. }