Form1.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:10k
源码类别:

C#编程

开发平台:

Others

  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.IO;
  8. using System.Net.Sockets;
  9. using System.Threading;
  10. namespace P2PChat
  11. {
  12. /// <summary>
  13. /// Form1 的摘要说明。
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.Button button1;
  18. private System.Windows.Forms.Button button2;
  19. private System.Windows.Forms.Button button3;
  20. private System.Windows.Forms.TextBox textBox1;
  21. private System.Windows.Forms.TextBox textBox2;
  22. private System.Windows.Forms.TextBox textBox3;
  23. private System.Windows.Forms.RichTextBox richTextBox1;
  24. private System.Windows.Forms.StatusBar statusBar1;
  25. private System.Windows.Forms.Label label1;
  26. private System.Windows.Forms.Label label2;
  27. private Thread th;
  28. private TcpListener tcpl;
  29. private bool listenerRun = true;
  30. private System.Windows.Forms.Label label3;
  31. /// <summary>
  32. /// 必需的设计器变量。
  33. /// </summary>
  34. private System.ComponentModel.Container components = null;
  35. public Form1()
  36. {
  37. //
  38. // Windows 窗体设计器支持所必需的
  39. //
  40. InitializeComponent();
  41. //
  42. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  43. //
  44. }
  45. /// <summary>
  46. /// 清理所有正在使用的资源。
  47. /// </summary>
  48. protected override void Dispose( bool disposing )
  49. {
  50. if( disposing )
  51. {
  52. if (components != null) 
  53. {
  54. components.Dispose();
  55. }
  56. }
  57. base.Dispose( disposing );
  58. }
  59. #region Windows Form Designer generated code
  60. /// <summary>
  61. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  62. /// 此方法的内容。
  63. /// </summary>
  64. private void InitializeComponent()
  65. {
  66. this.button1 = new System.Windows.Forms.Button();
  67. this.button2 = new System.Windows.Forms.Button();
  68. this.button3 = new System.Windows.Forms.Button();
  69. this.textBox1 = new System.Windows.Forms.TextBox();
  70. this.textBox2 = new System.Windows.Forms.TextBox();
  71. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  72. this.statusBar1 = new System.Windows.Forms.StatusBar();
  73. this.label1 = new System.Windows.Forms.Label();
  74. this.label2 = new System.Windows.Forms.Label();
  75. this.textBox3 = new System.Windows.Forms.TextBox();
  76. this.label3 = new System.Windows.Forms.Label();
  77. this.SuspendLayout();
  78. // 
  79. // button1
  80. // 
  81. this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  82. this.button1.Location = new System.Drawing.Point(24, 56);
  83. this.button1.Name = "button1";
  84. this.button1.Size = new System.Drawing.Size(75, 32);
  85. this.button1.TabIndex = 0;
  86. this.button1.Text = "开始侦听";
  87. this.button1.Click += new System.EventHandler(this.button1_Click);
  88. // 
  89. // button2
  90. // 
  91. this.button2.Enabled = false;
  92. this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  93. this.button2.Location = new System.Drawing.Point(24, 96);
  94. this.button2.Name = "button2";
  95. this.button2.Size = new System.Drawing.Size(75, 32);
  96. this.button2.TabIndex = 1;
  97. this.button2.Text = "停止侦听";
  98. this.button2.Click += new System.EventHandler(this.button2_Click);
  99. // 
  100. // button3
  101. // 
  102. this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  103. this.button3.Location = new System.Drawing.Point(24, 136);
  104. this.button3.Name = "button3";
  105. this.button3.Size = new System.Drawing.Size(75, 32);
  106. this.button3.TabIndex = 2;
  107. this.button3.Text = "发送消息";
  108. this.button3.Click += new System.EventHandler(this.button3_Click);
  109. // 
  110. // textBox1
  111. // 
  112. this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  113. | System.Windows.Forms.AnchorStyles.Right)));
  114. this.textBox1.Location = new System.Drawing.Point(136, 8);
  115. this.textBox1.Name = "textBox1";
  116. this.textBox1.Size = new System.Drawing.Size(280, 21);
  117. this.textBox1.TabIndex = 3;
  118. this.textBox1.Text = "";
  119. // 
  120. // textBox2
  121. // 
  122. this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  123. | System.Windows.Forms.AnchorStyles.Right)));
  124. this.textBox2.Location = new System.Drawing.Point(136, 56);
  125. this.textBox2.Multiline = true;
  126. this.textBox2.Name = "textBox2";
  127. this.textBox2.Size = new System.Drawing.Size(280, 112);
  128. this.textBox2.TabIndex = 4;
  129. this.textBox2.Text = "";
  130. this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
  131. // 
  132. // richTextBox1
  133. // 
  134. this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  135. | System.Windows.Forms.AnchorStyles.Left) 
  136. | System.Windows.Forms.AnchorStyles.Right)));
  137. this.richTextBox1.ForeColor = System.Drawing.Color.SteelBlue;
  138. this.richTextBox1.Location = new System.Drawing.Point(24, 200);
  139. this.richTextBox1.Name = "richTextBox1";
  140. this.richTextBox1.ReadOnly = true;
  141. this.richTextBox1.Size = new System.Drawing.Size(392, 152);
  142. this.richTextBox1.TabIndex = 5;
  143. this.richTextBox1.Text = "";
  144. this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged);
  145. // 
  146. // statusBar1
  147. // 
  148. this.statusBar1.Location = new System.Drawing.Point(0, 351);
  149. this.statusBar1.Name = "statusBar1";
  150. this.statusBar1.Size = new System.Drawing.Size(440, 22);
  151. this.statusBar1.TabIndex = 6;
  152. // 
  153. // label1
  154. // 
  155. this.label1.Location = new System.Drawing.Point(24, 0);
  156. this.label1.Name = "label1";
  157. this.label1.Size = new System.Drawing.Size(72, 32);
  158. this.label1.TabIndex = 7;
  159. this.label1.Text = "目标地址:";
  160. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  161. // 
  162. // label2
  163. // 
  164. this.label2.Location = new System.Drawing.Point(32, 32);
  165. this.label2.Name = "label2";
  166. this.label2.Size = new System.Drawing.Size(48, 16);
  167. this.label2.TabIndex = 8;
  168. this.label2.Text = "昵称:";
  169. this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  170. // 
  171. // textBox3
  172. // 
  173. this.textBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  174. | System.Windows.Forms.AnchorStyles.Right)));
  175. this.textBox3.ForeColor = System.Drawing.Color.Firebrick;
  176. this.textBox3.Location = new System.Drawing.Point(136, 32);
  177. this.textBox3.Name = "textBox3";
  178. this.textBox3.Size = new System.Drawing.Size(280, 21);
  179. this.textBox3.TabIndex = 9;
  180. this.textBox3.Text = "";
  181. // 
  182. // label3
  183. // 
  184. this.label3.Location = new System.Drawing.Point(24, 176);
  185. this.label3.Name = "label3";
  186. this.label3.Size = new System.Drawing.Size(80, 16);
  187. this.label3.TabIndex = 10;
  188. this.label3.Text = "聊天纪录:";
  189. this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  190. // 
  191. // Form1
  192. // 
  193. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  194. this.ClientSize = new System.Drawing.Size(440, 373);
  195. this.Controls.Add(this.label3);
  196. this.Controls.Add(this.textBox3);
  197. this.Controls.Add(this.label2);
  198. this.Controls.Add(this.label1);
  199. this.Controls.Add(this.statusBar1);
  200. this.Controls.Add(this.richTextBox1);
  201. this.Controls.Add(this.textBox2);
  202. this.Controls.Add(this.textBox1);
  203. this.Controls.Add(this.button3);
  204. this.Controls.Add(this.button2);
  205. this.Controls.Add(this.button1);
  206. this.Name = "Form1";
  207. this.Text = "P2P聊天工具";
  208. this.ResumeLayout(false);
  209. }
  210. #endregion
  211. private void Listen()
  212. {
  213. try
  214. {
  215. tcpl = new TcpListener(5656);
  216. tcpl.Start();
  217. statusBar1.Text = "正在监听...";
  218. while(listenerRun)
  219. {
  220. Socket s = tcpl.AcceptSocket();
  221. Byte[] stream = new Byte[80];
  222. int i=s.Receive(stream) ;
  223. string message = System.Text.Encoding.UTF8.GetString(stream);
  224. richTextBox1.AppendText(message);
  225. }
  226. }
  227. catch(System.Security.SecurityException)
  228. {
  229. MessageBox.Show("防火墙安全错误!","错误",
  230. MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  231. }
  232. catch(Exception)
  233. {
  234. statusBar1.Text = "已停止监听!";
  235. }
  236. }
  237. private void Send()
  238. {
  239. try
  240. {
  241. string msg = "<"+textBox3.Text+">"+textBox2.Text;
  242. TcpClient tcpc = new TcpClient(textBox1.Text, 5656);
  243. NetworkStream tcpStream = tcpc.GetStream();
  244. StreamWriter reqStreamW = new StreamWriter(tcpStream);
  245. reqStreamW.Write(msg);
  246. reqStreamW.Flush();
  247. tcpStream.Close();
  248. tcpc.Close();
  249. richTextBox1.AppendText(msg);
  250. textBox2.Clear();
  251. }
  252. catch(Exception)
  253. {
  254. statusBar1.Text = "目标计算机拒绝连接请求!";
  255. }
  256. }
  257. private void Stop()
  258. {
  259. tcpl.Stop();
  260. th.Abort();
  261. }
  262.         
  263. /// <summary>
  264. /// 应用程序的主入口点。
  265. /// </summary>
  266. [STAThread]
  267. static void Main() 
  268. {
  269. Application.Run(new Form1());
  270. }
  271. private void button1_Click(object sender, System.EventArgs e)
  272. {
  273. button1.Enabled = false;
  274. button2.Enabled = true;
  275. th = new Thread(new ThreadStart(Listen));
  276. th.Start();
  277. }
  278. private void button2_Click(object sender, System.EventArgs e)
  279. {
  280. button1.Enabled = true;
  281. button2.Enabled = false;
  282. listenerRun = false;
  283. Stop();
  284. }
  285. private void button3_Click(object sender, System.EventArgs e)
  286. {
  287. Send();
  288. }
  289. private void textBox2_TextChanged(object sender, System.EventArgs e)
  290. {
  291. }
  292. private void richTextBox1_TextChanged(object sender, System.EventArgs e)
  293. {
  294. }
  295. }
  296. }