自定义事件的使用例程

发表于:2007-06-30来源:作者:点击数: 标签:
自定义事件的使 用例 程 蓝色力量 转贴请注明: 开发 者俱乐部() 2002.1.22 这段程序运用一个sever类设计一个聊天室: //此类为 服务器 类-用于与客户端的通信,其中构造了几个自定义事件 using System;using System.Net; using System.Net.Sockets; using
自定义事件的使用例

蓝色力量 转贴请注明:开发者俱乐部()

2002.1.22
这段程序运用一个sever类设计一个聊天室:
//此类为服务器类-用于与客户端的通信,其中构造了几个自定义事件
using System;using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace sockchat
{
    public delegate void EventServerStartHandler(EventServerStartArgs e);  //声明代理
    public delegate void EventClientContextHandler(EventClientContextArgs e);
    public class EventClientContextArgs //构造自定义事件类
    {
        public string IP;
        public EventClientContextArgs(string _ip)
        {
            IP = _ip;
        }
    }
    public class EventServerStartArgs :EventArgs
    {
        public string returnstring;
        public EventServerStartArgs()
        {
            returnstring = "服务器已启动";
        }        
    }

        public class server
        {
            public event EventServerStartHandler SStartEvent;
            public event EventClientContextHandler CcontextEvent;
            private TcpListener tcpserver;
            private Thread thread;
            private int _port;
            private Socket socket;
            public int port
            {
                get
                {
                    return _port;
                }
            }
            public server(int c_port)
            {
                _port = c_port;
                tcpserver = new TcpListener(_port);
                thread = null;
            }
            public bool start()
            {
                
                bool s = false;
                try
                {
                    tcpserver.Start();                    
                    thread = new Thread(new ThreadStart(this.listener));
                    thread.Start();
                    s = true;
                }
                catch(System.Exception e)
                {
                    s = false;
                }
                if(s)
                {
                    this.SStartEvent(new EventServerStartArgs());
                }
                else
                {
                    
                }
                
                return s;
            }
            public void listener()
            {
                while(true)
                {
                    clientcontent(tcpserver.Aclearcase/" target="_blank" >cceptSocket());
                    
                }
            }
            public void clientcontent(Socket s)
            {
                socket = s;
                this.CcontextEvent(new EventClientContextArgs("202.199.333.111"));                            
            }
            public void send(string s)
            {
                byte[] sendstring = System.Text.Encoding.Unicode.GetBytes(s.ToCharArray());
                socket.Send(sendstring);
            }

        }
    }
//此为具体设计的一个窗体,绑了Server类的几个事件,发生相用的功能。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace sockchat
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.TextBox textBox2;
  private server ser;

  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;
  public Form2()
  {
   ser = new server(8888);
   ser.SStartEvent += new EventServerStartHandler(this.serverstartevent);
   ser.CcontextEvent += new EventClientContextHandler(this.clientcontextevent);
   
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();
   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }
  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.button2 = new System.Windows.Forms.Button();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.button3 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(8, 8);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "启动服务";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(8, 40);
   this.textBox1.Multiline = true;
   this.textBox1.Name = "textBox1";
   this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
   this.textBox1.Size = new System.Drawing.Size(304, 144);
   this.textBox1.TabIndex = 1;
   this.textBox1.Text = "";
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(8, 192);
   this.button2.Name = "button2";
   this.button2.TabIndex = 2;
   this.button2.Text = "发送消息";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // textBox2
   //
   this.textBox2.Location = new System.Drawing.Point(8, 224);
   this.textBox2.Name = "textBox2";
   this.textBox2.Size = new System.Drawing.Size(304, 21);
   this.textBox2.TabIndex = 3;
   this.textBox2.Text = "";
   //
   // Form2
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(320, 253);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
                    this.button3,
                    this.textBox2,
                    this.button2,
                    this.textBox1,
                    this.button1});
   this.Name = "Form2";
   this.Text = "服务器端程序";
   this.ResumeLayout(false);
  }
  #endregion
  private void button1_Click(object sender, System.EventArgs e)
  {
   
   ser.start();
   
  }
  private void serverstartevent(EventServerStartArgs e)
  {
   textBox1.AppendText(e.returnstring + "\n");
  }
  private void clientcontextevent(EventClientContextArgs e)
  {
   textBox1.AppendText(e.IP + "连接到服务器\n");
  }
  private void button2_Click(object sender, System.EventArgs e)
  {
   string send = textBox2.Text;
   ser.send(send);
  }
  
}
}

原文转自:http://www.ltesting.net