Form1.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.Data;
  7. using System.Net.Sockets;
  8. using System.Threading;
  9. using System.Net;
  10. namespace qqserver
  11. {
  12. /// <summary>
  13. /// Form1 的摘要说明。
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. string ip;
  18. private System.Windows.Forms.Button button1;
  19. private System.Windows.Forms.Button button2;
  20. public System.Windows.Forms.RichTextBox richTextBox1;
  21. public System.Collections.ArrayList userlist;
  22. public System.Collections.ArrayList Threadlist;
  23. private TcpListener listener;
  24. private Thread getusers;
  25. /// <summary>
  26. /// 必需的设计器变量。
  27. /// </summary>
  28. private System.ComponentModel.Container components = null;
  29. public Form1()
  30. {
  31. //
  32. // Windows 窗体设计器支持所必需的
  33. //
  34. userlist=new ArrayList();
  35. Threadlist=new ArrayList();
  36. InitializeComponent();
  37. getusers=new Thread(new ThreadStart(setup));
  38. //
  39. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  40. //
  41. }
  42. /// <summary>
  43. /// 清理所有正在使用的资源。
  44. /// </summary>
  45. protected override void Dispose( bool disposing )
  46. {
  47. if( disposing )
  48. {
  49. if (components != null) 
  50. {
  51. components.Dispose();
  52. }
  53. }
  54. base.Dispose( disposing );
  55. }
  56. #region Windows 窗体设计器生成的代码
  57. /// <summary>
  58. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  59. /// 此方法的内容。
  60. /// </summary>
  61. private void InitializeComponent()
  62. {
  63. this.button1 = new System.Windows.Forms.Button();
  64. this.button2 = new System.Windows.Forms.Button();
  65. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  66. this.SuspendLayout();
  67. // 
  68. // button1
  69. // 
  70. this.button1.Location = new System.Drawing.Point(32, 24);
  71. this.button1.Name = "button1";
  72. this.button1.Size = new System.Drawing.Size(72, 40);
  73. this.button1.TabIndex = 0;
  74. this.button1.Text = "start";
  75. this.button1.Click += new System.EventHandler(this.button1_Click);
  76. // 
  77. // button2
  78. // 
  79. this.button2.Location = new System.Drawing.Point(160, 24);
  80. this.button2.Name = "button2";
  81. this.button2.Size = new System.Drawing.Size(72, 40);
  82. this.button2.TabIndex = 1;
  83. this.button2.Text = "stop";
  84. this.button2.Click += new System.EventHandler(this.button2_Click);
  85. // 
  86. // richTextBox1
  87. // 
  88. this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  89. | System.Windows.Forms.AnchorStyles.Left) 
  90. | System.Windows.Forms.AnchorStyles.Right)));
  91. this.richTextBox1.Location = new System.Drawing.Point(8, 88);
  92. this.richTextBox1.Name = "richTextBox1";
  93. this.richTextBox1.Size = new System.Drawing.Size(324, 224);
  94. this.richTextBox1.TabIndex = 2;
  95. this.richTextBox1.Text = "";
  96. // 
  97. // Form1
  98. // 
  99. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  100. this.ClientSize = new System.Drawing.Size(336, 325);
  101. this.Controls.Add(this.richTextBox1);
  102. this.Controls.Add(this.button2);
  103. this.Controls.Add(this.button1);
  104. this.Name = "Form1";
  105. this.Text = "qqserver";
  106. this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
  107. this.ResumeLayout(false);
  108. }
  109. #endregion
  110. /// <summary>
  111. /// 应用程序的主入口点。
  112. /// </summary>
  113. [STAThread]
  114. static void Main() 
  115. {
  116. Application.Run(new Form1());
  117. }
  118. private void button1_Click(object sender, System.EventArgs e)
  119. {
  120. try
  121. {
  122. if(getusers.ThreadState==ThreadState.Unstarted)
  123. {
  124. getusers.Start();
  125. }
  126. else
  127. getusers.Resume();
  128. this.button1.Enabled=false;
  129. }
  130. catch(Exception ee)
  131. {
  132. MessageBox.Show(ee.ToString());
  133. }
  134. }
  135. public void setup()
  136. {
  137. try
  138. {
  139. string hostname=Dns.GetHostName();
  140. IPAddress ipAddress = Dns.GetHostByName(hostname).AddressList[0];
  141. MessageBox.Show(""+ipAddress.ToString());
  142. this.listener=new TcpListener(ipAddress,5000);
  143. }
  144. catch(Exception ee)
  145. {
  146. MessageBox.Show(ee.ToString());
  147. }
  148. listener.Start();
  149. while(true)
  150. {
  151. user users=new user(listener.AcceptSocket(),this);
  152. userlist.Add(users);
  153. Thread thread=new Thread(new ThreadStart(users.run ));
  154. Threadlist.Add(thread);
  155. thread.Start();
  156. }
  157. }
  158. private void button2_Click(object sender, System.EventArgs e)
  159. {
  160. this.button1.Enabled=true;
  161. getusers.Suspend();
  162. }
  163. private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  164. {
  165. this.Threadlist.Clear();
  166. this.userlist.Clear();
  167. getusers.Abort();
  168. this.listener.Stop();
  169. }
  170. }
  171. }