自定义事件的使用例程

发布时间: 2007-05-21 09:44    作者: 未知    来源: 未知    浏览:    评论

自定义事件的使用例程

蓝色力量 cnhai@21cn.com 转贴请注明:开发者俱乐部(http://www.dev-club.com)

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.AcceptSocket());
                    
                }
            }
            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);
  }
  
}
}

TAG

Smile Big Smile Surprise Stick out tongue Wink Sad Tongue Tied Indifferent Crying Embarrassed Cool Angry Angel Devil [8-|] [:#] [:-*] [:^)] [<:o)] [|-)] Yes Beer Left Hug Music Star Time Snail Pizza Automobile Umbrella Computer Storm [mo] [8o|] [^o)] [+o(] [*-)] [8-)] Coffee No Drinks [Z] Right Hug Cake Broken Heart Gift Wilted Flower Movie Dog Idea Sleep Email Travel Paradise
呢称:

加粗 斜体 下划线 链接 图片 代码 邮件地址 引用 列表

最多只能输入100个字符

Tags

SQL 数据库 asp.net C# XML 控件 .NET教程 程序 事件 数据 安全 代码 Server 客户端 验证 数据库专栏 接口 文件 Oracle DataSet 函数 DataGrid 问题 .net return C#语言 JavaScript 服务 IIS 对象 语句 windows 继承 时间 web.config 设计 开发 参数 变量 解决 字符 ADO.net 环境 VB.Net语言 web 异常 工具 服务器 计算 实例 OLEDB Application VB Word WebService insert asp net 安装 记录

精华推荐

更多

精品下载

更多