Net.cs
上传用户:fuyouda
上传日期:2021-03-17
资源大小:8619k
文件大小:11k
源码类别:

Windows编程

开发平台:

C/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 WindowsApplication1
  11. {
  12. /// <summary>
  13. /// Net 的摘要说明。
  14. /// </summary>
  15. public class Net : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.Label label2;
  18. private System.Windows.Forms.Label label1;
  19. private System.Windows.Forms.TextBox textBox2;
  20. private System.Windows.Forms.Label label3;
  21. private System.Windows.Forms.Label label4;
  22. private System.Windows.Forms.RichTextBox richTextBox3;
  23. private System.Windows.Forms.Label label5;
  24. private System.Windows.Forms.RichTextBox richTextBox2;
  25. private System.Windows.Forms.Label label6;
  26. private System.Windows.Forms.RichTextBox richTextBox1;
  27. private System.Windows.Forms.Label label7;
  28. private System.Windows.Forms.Button button1;
  29. private System.Windows.Forms.TextBox textBox7;
  30. private System.Windows.Forms.TextBox textBox1;
  31. //声明监听线程对象
  32. private Thread MyListenThread;
  33. //声明网络监听对象
  34. private TcpListener MyTcpListener;
  35. /// <summary>
  36. /// 必需的设计器变量。
  37. /// </summary>
  38. private System.ComponentModel.Container components = null;
  39. public Net()
  40. {
  41. //
  42. // Windows 窗体设计器支持所必需的
  43. //
  44. InitializeComponent();
  45. //
  46. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  47. //
  48. }
  49. private void StartListen()
  50. {
  51. MyTcpListener=new TcpListener(5000);
  52. //开始监听
  53. MyTcpListener.Start();              
  54. while(true)
  55. {//获取TcpClient
  56. TcpClient MyTcpClient=MyTcpListener.AcceptTcpClient();
  57. NetworkStream MyStream=MyTcpClient.GetStream();
  58. byte[] MyBytes = new byte[1024];
  59. int MyBytesRead=MyStream.Read(MyBytes,0,MyBytes.Length);
  60. string MyMessage=System.Text.Encoding.Default.GetString(MyBytes,0,MyBytesRead);
  61. this.richTextBox2.Text+=MyMessage;
  62. }
  63. }
  64. /// <summary>
  65. /// 清理所有正在使用的资源。
  66. /// </summary>
  67. protected override void Dispose( bool disposing )
  68. {
  69. if( disposing )
  70. {
  71. if(components != null)
  72. {
  73. components.Dispose();
  74. }
  75. }
  76. base.Dispose( disposing );
  77. }
  78. #region Windows 窗体设计器生成的代码
  79. /// <summary>
  80. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  81. /// 此方法的内容。
  82. /// </summary>
  83. private void InitializeComponent()
  84. {
  85. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Net));
  86. this.label2 = new System.Windows.Forms.Label();
  87. this.textBox7 = new System.Windows.Forms.TextBox();
  88. this.label1 = new System.Windows.Forms.Label();
  89. this.textBox2 = new System.Windows.Forms.TextBox();
  90. this.label3 = new System.Windows.Forms.Label();
  91. this.textBox1 = new System.Windows.Forms.TextBox();
  92. this.label4 = new System.Windows.Forms.Label();
  93. this.richTextBox3 = new System.Windows.Forms.RichTextBox();
  94. this.label5 = new System.Windows.Forms.Label();
  95. this.richTextBox2 = new System.Windows.Forms.RichTextBox();
  96. this.label6 = new System.Windows.Forms.Label();
  97. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  98. this.label7 = new System.Windows.Forms.Label();
  99. this.button1 = new System.Windows.Forms.Button();
  100. this.SuspendLayout();
  101. // 
  102. // label2
  103. // 
  104. this.label2.Location = new System.Drawing.Point(188, -32);
  105. this.label2.Name = "label2";
  106. this.label2.Size = new System.Drawing.Size(48, 18);
  107. this.label2.TabIndex = 13;
  108. this.label2.Text = "姓名:";
  109. // 
  110. // textBox7
  111. // 
  112. this.textBox7.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  113. this.textBox7.ForeColor = System.Drawing.Color.Red;
  114. this.textBox7.Location = new System.Drawing.Point(12, -40);
  115. this.textBox7.Name = "textBox7";
  116. this.textBox7.Size = new System.Drawing.Size(168, 26);
  117. this.textBox7.TabIndex = 12;
  118. this.textBox7.Text = "";
  119. // 
  120. // label1
  121. // 
  122. this.label1.Location = new System.Drawing.Point(-92, -32);
  123. this.label1.Name = "label1";
  124. this.label1.Size = new System.Drawing.Size(104, 18);
  125. this.label1.TabIndex = 11;
  126. this.label1.Text = "接收主机IP地址:";
  127. // 
  128. // textBox2
  129. // 
  130. this.textBox2.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
  131. this.textBox2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  132. this.textBox2.ForeColor = System.Drawing.Color.Red;
  133. this.textBox2.Location = new System.Drawing.Point(344, 24);
  134. this.textBox2.Name = "textBox2";
  135. this.textBox2.Size = new System.Drawing.Size(144, 26);
  136. this.textBox2.TabIndex = 17;
  137. this.textBox2.Text = "";
  138. // 
  139. // label3
  140. // 
  141. this.label3.BackColor = System.Drawing.Color.Transparent;
  142. this.label3.Location = new System.Drawing.Point(296, 32);
  143. this.label3.Name = "label3";
  144. this.label3.Size = new System.Drawing.Size(48, 16);
  145. this.label3.TabIndex = 16;
  146. this.label3.Text = "姓名:";
  147. // 
  148. // textBox1
  149. // 
  150. this.textBox1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  151. this.textBox1.ForeColor = System.Drawing.Color.Red;
  152. this.textBox1.Location = new System.Drawing.Point(120, 24);
  153. this.textBox1.Name = "textBox1";
  154. this.textBox1.Size = new System.Drawing.Size(168, 26);
  155. this.textBox1.TabIndex = 15;
  156. this.textBox1.Text = "";
  157. // 
  158. // label4
  159. // 
  160. this.label4.BackColor = System.Drawing.Color.Transparent;
  161. this.label4.Location = new System.Drawing.Point(16, 32);
  162. this.label4.Name = "label4";
  163. this.label4.Size = new System.Drawing.Size(104, 16);
  164. this.label4.TabIndex = 14;
  165. this.label4.Text = "接收主机IP地址:";
  166. this.label4.Click += new System.EventHandler(this.label4_Click);
  167. // 
  168. // richTextBox3
  169. // 
  170. this.richTextBox3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  171. this.richTextBox3.ForeColor = System.Drawing.Color.Blue;
  172. this.richTextBox3.Location = new System.Drawing.Point(16, 80);
  173. this.richTextBox3.Name = "richTextBox3";
  174. this.richTextBox3.ReadOnly = true;
  175. this.richTextBox3.Size = new System.Drawing.Size(232, 152);
  176. this.richTextBox3.TabIndex = 19;
  177. this.richTextBox3.Text = "";
  178. // 
  179. // label5
  180. // 
  181. this.label5.BackColor = System.Drawing.Color.Transparent;
  182. this.label5.Location = new System.Drawing.Point(48, 64);
  183. this.label5.Name = "label5";
  184. this.label5.Size = new System.Drawing.Size(112, 16);
  185. this.label5.TabIndex = 18;
  186. this.label5.Text = "已经发送的内容:";
  187. // 
  188. // richTextBox2
  189. // 
  190. this.richTextBox2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  191. this.richTextBox2.ForeColor = System.Drawing.Color.Red;
  192. this.richTextBox2.Location = new System.Drawing.Point(288, 80);
  193. this.richTextBox2.Name = "richTextBox2";
  194. this.richTextBox2.ReadOnly = true;
  195. this.richTextBox2.Size = new System.Drawing.Size(208, 152);
  196. this.richTextBox2.TabIndex = 21;
  197. this.richTextBox2.Text = "";
  198. // 
  199. // label6
  200. // 
  201. this.label6.BackColor = System.Drawing.Color.Transparent;
  202. this.label6.Location = new System.Drawing.Point(280, 64);
  203. this.label6.Name = "label6";
  204. this.label6.Size = new System.Drawing.Size(112, 16);
  205. this.label6.TabIndex = 20;
  206. this.label6.Text = "已经收到的内容:";
  207. // 
  208. // richTextBox1
  209. // 
  210. this.richTextBox1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  211. this.richTextBox1.Location = new System.Drawing.Point(16, 264);
  212. this.richTextBox1.Name = "richTextBox1";
  213. this.richTextBox1.Size = new System.Drawing.Size(480, 88);
  214. this.richTextBox1.TabIndex = 23;
  215. this.richTextBox1.Text = "";
  216. // 
  217. // label7
  218. // 
  219. this.label7.BackColor = System.Drawing.Color.Transparent;
  220. this.label7.Location = new System.Drawing.Point(48, 248);
  221. this.label7.Name = "label7";
  222. this.label7.Size = new System.Drawing.Size(128, 16);
  223. this.label7.TabIndex = 22;
  224. this.label7.Text = "将要发送的内容:";
  225. // 
  226. // button1
  227. // 
  228. this.button1.BackColor = System.Drawing.Color.Transparent;
  229. this.button1.Location = new System.Drawing.Point(424, 360);
  230. this.button1.Name = "button1";
  231. this.button1.TabIndex = 24;
  232. this.button1.Text = "发送";
  233. this.button1.Click += new System.EventHandler(this.button1_Click);
  234. // 
  235. // Net
  236. // 
  237. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  238. this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
  239. this.ClientSize = new System.Drawing.Size(520, 392);
  240. this.Controls.Add(this.button1);
  241. this.Controls.Add(this.richTextBox1);
  242. this.Controls.Add(this.label7);
  243. this.Controls.Add(this.richTextBox2);
  244. this.Controls.Add(this.label6);
  245. this.Controls.Add(this.richTextBox3);
  246. this.Controls.Add(this.label5);
  247. this.Controls.Add(this.textBox2);
  248. this.Controls.Add(this.label3);
  249. this.Controls.Add(this.textBox1);
  250. this.Controls.Add(this.label4);
  251. this.Controls.Add(this.label2);
  252. this.Controls.Add(this.textBox7);
  253. this.Controls.Add(this.label1);
  254. this.Name = "Net";
  255. this.Text = "Net";
  256. this.Load += new System.EventHandler(this.Net_Load);
  257. this.Closed += new System.EventHandler(this.Net_Closed);
  258. this.ResumeLayout(false);
  259. }
  260. #endregion
  261. private void Net_Load(object sender, System.EventArgs e)
  262. {
  263. //开始监听
  264. //创建监听线程
  265. this.MyListenThread= new Thread(new ThreadStart(StartListen));
  266. //启动线程
  267. this.MyListenThread.Start();
  268. }
  269. private void label4_Click(object sender, System.EventArgs e)
  270. {
  271. }
  272. private void button1_Click(object sender, System.EventArgs e)
  273. {
  274. //发送聊天内容
  275. if(this.textBox1.Text.Length<1||this.textBox2.Text.Length<1||this.richTextBox1.Text.Length<1)
  276. return;
  277. try
  278. {
  279. string MyMessage=DateTime.Now.ToString()+"n"+this.textBox2.Text + ":" + this.richTextBox1.Text + "n";
  280. //根据目标计算机地址建立连接
  281. TcpClient MyTcpClient = new TcpClient(this.textBox1.Text, 5000);
  282. //获得用于网络访问的数据流
  283. NetworkStream MyTcpStream =MyTcpClient.GetStream();
  284. StreamWriter MyStream = new StreamWriter(MyTcpStream);
  285. //将字符串写入流
  286. MyStream.Write(MyMessage,System.Text.Encoding.Default);
  287. //将缓冲数据写入基础流
  288. MyStream.Flush();
  289. //关闭网络流
  290. MyStream.Close();
  291. MyTcpClient.Close(); 
  292. this.richTextBox3.AppendText(MyMessage);
  293. this.richTextBox1.Clear();
  294. }
  295. catch(Exception Err)
  296. {
  297. MessageBox.Show(Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  298. }
  299. }
  300. private void Net_Closed(object sender, System.EventArgs e)
  301. {
  302. //关闭SOCKET
  303. try
  304. {
  305. if (this.MyTcpListener!=null)
  306. {//关闭监听器
  307. this.MyTcpListener.Stop();
  308. }
  309. if (this.MyListenThread!=null)
  310. { //如果线程还处于运行状态就关闭它
  311. if (this.MyListenThread.ThreadState==ThreadState.Running)
  312. {
  313. this.MyListenThread.Abort();
  314. }
  315. }
  316. }
  317. catch(Exception Err)
  318. {
  319. MessageBox.Show(Err.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  320. }
  321. }
  322. }
  323. }