Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 9k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Threading;
  8. using System.Net.Sockets;
  9. using System.IO;
  10. namespace ch7_2
  11. {
  12. /// <summary>
  13. /// Form1 的摘要说明。
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.RichTextBox richTextBox1;
  18. private System.Windows.Forms.TextBox textBox1;
  19. private System.Windows.Forms.Label label1;
  20. private System.Windows.Forms.Button button1;
  21. private System.Windows.Forms.Button button2;
  22. private System.Windows.Forms.Label label2;
  23. private System.Windows.Forms.TextBox textBox2;
  24. private System.Windows.Forms.Button button3;
  25. private System.Windows.Forms.GroupBox groupBox1;
  26. private System.Windows.Forms.StatusBar statusBar1;
  27.         
  28. //代表监听的线程
  29. private Thread listenThread;
  30. //代表监听的状态
  31. private bool listen=false;
  32.         //代表网络监听的对象
  33. private TcpListener tcpListener;
  34. private System.Windows.Forms.RichTextBox richTextBox2;
  35. /// <summary>
  36. /// 必需的设计器变量。
  37. /// </summary>
  38. private System.ComponentModel.Container components = null;
  39. public Form1()
  40. {
  41. //
  42. // Windows 窗体设计器支持所必需的
  43. //
  44. InitializeComponent();
  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 Form Designer generated code
  64. /// <summary>
  65. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  66. /// 此方法的内容。
  67. /// </summary>
  68. private void InitializeComponent()
  69. {
  70. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  71. this.textBox1 = new System.Windows.Forms.TextBox();
  72. this.label1 = new System.Windows.Forms.Label();
  73. this.button1 = new System.Windows.Forms.Button();
  74. this.button2 = new System.Windows.Forms.Button();
  75. this.label2 = new System.Windows.Forms.Label();
  76. this.textBox2 = new System.Windows.Forms.TextBox();
  77. this.button3 = new System.Windows.Forms.Button();
  78. this.groupBox1 = new System.Windows.Forms.GroupBox();
  79. this.statusBar1 = new System.Windows.Forms.StatusBar();
  80. this.richTextBox2 = new System.Windows.Forms.RichTextBox();
  81. this.groupBox1.SuspendLayout();
  82. this.SuspendLayout();
  83. // 
  84. // richTextBox1
  85. // 
  86. this.richTextBox1.Location = new System.Drawing.Point(16, 8);
  87. this.richTextBox1.Name = "richTextBox1";
  88. this.richTextBox1.Size = new System.Drawing.Size(272, 40);
  89. this.richTextBox1.TabIndex = 0;
  90. this.richTextBox1.Text = "";
  91. // 
  92. // textBox1
  93. // 
  94. this.textBox1.Location = new System.Drawing.Point(88, 20);
  95. this.textBox1.Name = "textBox1";
  96. this.textBox1.Size = new System.Drawing.Size(176, 21);
  97. this.textBox1.TabIndex = 1;
  98. this.textBox1.Text = "";
  99. // 
  100. // label1
  101. // 
  102. this.label1.Location = new System.Drawing.Point(16, 24);
  103. this.label1.Name = "label1";
  104. this.label1.Size = new System.Drawing.Size(72, 16);
  105. this.label1.TabIndex = 2;
  106. this.label1.Text = "目标地址";
  107. // 
  108. // button1
  109. // 
  110. this.button1.Location = new System.Drawing.Point(104, 56);
  111. this.button1.Name = "button1";
  112. this.button1.Size = new System.Drawing.Size(72, 24);
  113. this.button1.TabIndex = 3;
  114. this.button1.Text = "准备接收";
  115. this.button1.Click += new System.EventHandler(this.button1_Click);
  116. // 
  117. // button2
  118. // 
  119. this.button2.Enabled = false;
  120. this.button2.Location = new System.Drawing.Point(190, 56);
  121. this.button2.Name = "button2";
  122. this.button2.TabIndex = 4;
  123. this.button2.Text = "停止接收";
  124. this.button2.Click += new System.EventHandler(this.button2_Click);
  125. // 
  126. // label2
  127. // 
  128. this.label2.Location = new System.Drawing.Point(24, 62);
  129. this.label2.Name = "label2";
  130. this.label2.Size = new System.Drawing.Size(72, 16);
  131. this.label2.TabIndex = 5;
  132. this.label2.Text = "请输入姓名";
  133. // 
  134. // textBox2
  135. // 
  136. this.textBox2.Location = new System.Drawing.Point(104, 56);
  137. this.textBox2.Name = "textBox2";
  138. this.textBox2.TabIndex = 6;
  139. this.textBox2.Text = "";
  140. // 
  141. // button3
  142. // 
  143. this.button3.Location = new System.Drawing.Point(224, 56);
  144. this.button3.Name = "button3";
  145. this.button3.Size = new System.Drawing.Size(56, 23);
  146. this.button3.TabIndex = 7;
  147. this.button3.Text = "发送";
  148. this.button3.Click += new System.EventHandler(this.button3_Click);
  149. // 
  150. // groupBox1
  151. // 
  152. this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  153. this.label1,
  154. this.textBox1,
  155. this.button1,
  156. this.button2});
  157. this.groupBox1.Location = new System.Drawing.Point(16, 192);
  158. this.groupBox1.Name = "groupBox1";
  159. this.groupBox1.Size = new System.Drawing.Size(272, 96);
  160. this.groupBox1.TabIndex = 8;
  161. this.groupBox1.TabStop = false;
  162. // 
  163. // statusBar1
  164. // 
  165. this.statusBar1.Location = new System.Drawing.Point(0, 303);
  166. this.statusBar1.Name = "statusBar1";
  167. this.statusBar1.Size = new System.Drawing.Size(296, 22);
  168. this.statusBar1.TabIndex = 9;
  169. // 
  170. // richTextBox2
  171. // 
  172. this.richTextBox2.Location = new System.Drawing.Point(16, 88);
  173. this.richTextBox2.Name = "richTextBox2";
  174. this.richTextBox2.Size = new System.Drawing.Size(272, 96);
  175. this.richTextBox2.TabIndex = 10;
  176. this.richTextBox2.Text = "";
  177. // 
  178. // Form1
  179. // 
  180. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  181. this.ClientSize = new System.Drawing.Size(296, 325);
  182. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  183.   this.richTextBox2,
  184.   this.statusBar1,
  185.   this.groupBox1,
  186.   this.button3,
  187.   this.textBox2,
  188.   this.label2,
  189.   this.richTextBox1});
  190. this.Name = "Form1";
  191. this.Text = "点对点聊天";
  192. this.Closed += new System.EventHandler(this.Form1_Closed);
  193. this.groupBox1.ResumeLayout(false);
  194. this.ResumeLayout(false);
  195. }
  196. #endregion
  197. /// <summary>
  198. /// 应用程序的主入口点。
  199. /// </summary>
  200. [STAThread]
  201. static void Main() 
  202. {
  203. Application.Run(new Form1());
  204. }
  205. private void button1_Click(object sender, System.EventArgs e)
  206. {
  207. //改变按钮状态
  208. button1.Enabled = false;
  209. button2.Enabled = true;
  210. //创建监听线程
  211. listenThread = new Thread(new ThreadStart(StartListen));
  212. //启动线程
  213. listenThread.Start();
  214. }
  215. private void StartListen()
  216. {
  217. try
  218. {
  219. tcpListener = new TcpListener(5675);
  220. //开始监听
  221. tcpListener.Start();
  222. //改变状态栏 
  223. statusBar1.Text = "正在监听...";
  224. //设置监听状态
  225. this.listen=true;
  226.                 
  227. while(listen)
  228. {
  229. // //接受连接请求
  230. // Socket s = tcpListener.AcceptSocket();
  231. //                    //设置一个缓冲区
  232. // Byte[] stream = new Byte[100];
  233. // int i=s.Receive(stream) ;
  234. // //将获得的流转化为字符串类型
  235. // string message = System.Text.Encoding.UTF8.GetString(stream);
  236. // this.richTextBox2.AppendText(message);
  237. //获取TcpClient
  238. TcpClient s=tcpListener.AcceptTcpClient();
  239. NetworkStream nc=s.GetStream();
  240. byte[] bytes = new byte[1024];
  241. int bytesRead=nc.Read(bytes,0,bytes.Length);
  242. string message=System.Text.Encoding.Default.GetString(bytes,0,bytesRead);
  243. this.richTextBox2.AppendText(message);
  244. }
  245. }
  246. //错误处理
  247. catch 
  248. {
  249. //忽略错误处理
  250. //改变状态栏
  251. statusBar1.Text = "已停止监听!";
  252. }
  253. }
  254. private void button2_Click(object sender, System.EventArgs e)
  255. {
  256.    //改变按钮状态
  257.    this.button1.Enabled=true;
  258.    this.button2.Enabled=false;
  259.    //改变监听状态
  260.    this.listen=false;
  261.    //停止监听器
  262.    this.tcpListener.Stop();
  263.    //停止监听线程
  264.    this.listenThread.Abort();
  265. }
  266. private void button3_Click(object sender, System.EventArgs e)
  267. {
  268. try
  269. {
  270. //获得消息
  271. string msg = "<"+this.textBox2.Text+">"+this.richTextBox1.Text+"n";
  272. //根据目标计算机地址建立连接
  273. TcpClient tcpClient = new TcpClient(textBox1.Text, 5675);
  274. //获得用于网络访问的数据流
  275. NetworkStream tcpStream = tcpClient.GetStream();
  276. StreamWriter streamW = new StreamWriter(tcpStream);
  277. //将字符串写入流
  278. streamW.Write(msg);
  279. //将缓冲数据写入基础流
  280. streamW.Flush();
  281. //关闭网络流
  282. tcpStream.Close();
  283. //关闭
  284. tcpClient.Close();
  285.  
  286. this.richTextBox2.AppendText(msg);
  287. this.richTextBox1.Clear();
  288. }
  289. catch
  290. {
  291. statusBar1.Text =  "目标计算机拒绝连接请求!";
  292. }
  293. }
  294. private void Form1_Closed(object sender, System.EventArgs e)
  295. {
  296. if (this.tcpListener!=null)
  297. {
  298. //关闭监听器
  299. this.tcpListener.Stop();
  300. }
  301. if (this.listenThread!=null)
  302. {
  303. //如果线程还处于运行状态就关闭它
  304. if (this.listenThread.ThreadState==ThreadState.Running)
  305. {
  306. this.listenThread.Abort();
  307. }
  308. }
  309. }
  310. }
  311. }