using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net;
using System.Net.Sockets;namespace serverManager
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox txtHost;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.ListBox lstInfo; private int port;
private TcpListener listener;
private Socket acceptSocket;
private Thread thread;
private bool isConnection = false;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.lstInfo = new System.Windows.Forms.ListBox();
this.txtHost = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.txtPort = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(448, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 23);
this.button1.TabIndex = 0;
this.button1.Text = "停止服务";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(352, 16);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 23);
this.button2.TabIndex = 2;
this.button2.Text = "启动服务";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// lstInfo
// 
this.lstInfo.ItemHeight = 12;
this.lstInfo.Location = new System.Drawing.Point(8, 56);
this.lstInfo.Name = "lstInfo";
this.lstInfo.Size = new System.Drawing.Size(544, 292);
this.lstInfo.TabIndex = 4;
// 
// txtHost
// 
this.txtHost.Location = new System.Drawing.Point(80, 16);
this.txtHost.Name = "txtHost";
this.txtHost.Size = new System.Drawing.Size(128, 21);
this.txtHost.TabIndex = 5;
this.txtHost.Text = "192.168.1.108";
// 
// label1
// 
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
| System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 6;
this.label1.Text = "服务器地址";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
// 
// txtPort
// 
this.txtPort.Location = new System.Drawing.Point(280, 16);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(56, 21);
this.txtPort.TabIndex = 7;
this.txtPort.Text = "8003";
// 
// label2
// 
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
| System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.label2.Location = new System.Drawing.Point(224, 16);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 23);
this.label2.TabIndex = 8;
this.label2.Text = "端口";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(560, 357);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtHost);
this.Controls.Add(this.lstInfo);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion [STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{

} private void button2_Click(object sender, System.EventArgs e)
{
this.port = Convert.ToInt16(txtPort.Text);
string myip = txtHost.Text;
try
{
IPAddress ipAdd=IPAddress.Parse(myip);
//创建服务器套接字
listener=new TcpListener(ipAdd , port);
//开始监听服务器端口
listener.Start();
this.addServerMsg("服务器已经启动,正在监听..." + txtHost.Text + ":" + txtPort.Text);
thread= new Thread(new ThreadStart(this.StartListen));
thread.Start();
button2.Enabled=false;
this.isConnection = true;
}
catch(Exception ex)
{
this.addServerMsg(ex.Message);
}
} public void StartListen()
{ while(this.isConnection)
{
try
{
if(this.isConnection)
{
acceptSocket = this.listener.AcceptSocket();
Thread clientThread = new Thread(new ThreadStart(this.clientAction));
clientThread.Start();
}
else
{
}
}
catch(Exception ex)
{
this.addServerMsg(ex.Message);
}
} } private void stopServer()
{
if(this.isConnection)
{
this.thread.Abort();
this.listener.Stop();
this.isConnection = false;
this.addServerMsg("服务器已经停止..." + txtHost.Text + ":" + txtPort.Text);
this.button2.Enabled = true;
}
} private void clientAction()
{

} private void button1_Click(object sender, System.EventArgs e)
{
this.stopServer();
} private void addServerMsg(string msg)
{
lstInfo.Items.Add(msg);
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.stopServer();
Application.Exit();
}
}
}

解决方案 »

  1.   

    没仔细看,如果是因为在多线程里操作UI而且是用.net2.0的话需要调用Control.Invoke来操作控件,不然就会出现这个错误,而且在多线程里操作ui是不被推荐的
      

  2.   

    private void stopServer()
    {
    if(this.isConnection)
    {
    this.thread.Abort();
    this.listener.Stop();
    this.isConnection = false;
    this.addServerMsg("服务器已经停止..." + txtHost.Text + ":" + txtPort.Text);
    this.button2.Enabled = true;
    } -------------------------------------》》》》                  private void stopServer()
    {
    if(this.isConnection)
    {
    this.thread.Abort();
    this.listener.Stop();
    this.isConnection = false;
    thread.Suspend();
    this.addServerMsg("服务器已经停止..." + txtHost.Text + ":" + txtPort.Text);
    this.button2.Enabled = true;
    }
      

  3.   

    this.thread.Abort();之前thread.Suspend();更好。
      

  4.   

    谢谢 zeusvenus(清柳)(C#/ASP.NET)
    写把线程挂起吗?
    但是按照以上做后,又有另一个问题
    在启动,然后关闭服务后,执行closeing的时候,不能执行这个application.exit()