自动触发
/////////////
你也要对某一端口进行监听,当对这一端口有Socket请求时才会触发.参考一个列子,对本地10101端口进行监听:using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;public class StateObject 
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();  
}public class AsynchronousSocketListener 
{
public static string data = null;
public static ManualResetEvent allDone = new ManualResetEvent( false ); public AsynchronousSocketListener(){} public static void StartListening() 
{
byte[] bytes = new Byte[1024];
IPHostEntry ipHostInfo = Dns.Resolve( Dns.GetHostName() );
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint( ipAddress, 10101);
Socket listener = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
try 
{
listener.Bind( localEndPoint );
listener.Listen( 100 );
while ( true ) 
{
allDone.Reset();
Console.WriteLine( "Waiting for a connection..." );
listener.BeginAccept( new AsyncCallback( AcceptCallback ), listener );
allDone.WaitOne();
} } 
catch ( Exception e ) 
{
Console.WriteLine( e.ToString() );
} Console.WriteLine( "\nPress ENTER to continue..." );
Console.Read();
        
} public static void AcceptCallback( IAsyncResult ar ) 
{
allDone.Set();
Socket listener = ( Socket ) ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback( ReadCallback ), state);
} public static void ReadCallback( IAsyncResult ar ) 
{
String content = String.Empty;
StateObject state = (StateObject) ar.AsyncState;
Socket handler = state.workSocket;
int bytesRead = handler.EndReceive( ar );
if ( bytesRead > 0 ) 
{
state.sb.Append( Encoding.ASCII.GetString( state.buffer, 0, bytesRead ) );
content = state.sb.ToString();
if ( content.IndexOf(".") > -1 ) 
{
Console.WriteLine( "Read {0} bytes from socket. \n Data : {1}", content.Length, content );
Send( handler, "ok,sentok" );

else 
{
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback( ReadCallback ), state );
}
}
}
    
private static void Send( Socket handler, String data)  
{
byte[] byteData = Encoding.ASCII.GetBytes( data );
handler.BeginSend( byteData, 0, byteData.Length, 0, new AsyncCallback( SendCallback ), handler );
} private static void SendCallback( IAsyncResult ar ) 
{
try 
{
Socket handler = ( Socket ) ar.AsyncState;
int bytesSent = handler.EndSend(ar);
Console.WriteLine( "Sent {0} bytes to client.", bytesSent );
handler.Shutdown( SocketShutdown.Both );
handler.Close(); } 
catch ( Exception e ) 
{
Console.WriteLine( e.ToString() );
}
}
public static int Main(String[] args) 
{
StartListening();
return 0;
}
}

解决方案 »

  1.   

    要全面了解自定义事件的原理,你需要学习有关delegate的知识。(在下一个版本中自定义事件会更容易一些,但是至少现在不行)通常自定义事件有下面的几个步骤:1、声明一个delegate: (用于事件的类型的定义)
      如:public delegate void 事件名称EventHandler(object serder, EventArgs e);
      //事件名称用你的自己的来代替,随后的EventHandler是C#的建议命名规范,当然如果你不想遵守,可以使用任何字符甚至可以不要。   如果你想自定义事件的参数EventArgs,你可以从这个类派生你自己的事件参数类,然后在delegate的声明中,用你的参数类替换EventArgs2、在你的类中声明一个事件,并且使用步骤1的delegate作为事件的类型:
       public event 事件名称EventHandler 事件名称;3、在你的类中需要触发事件的方法中,添加事件触发代码:
       事件名称(this, new EventArgs());  
       或者:
        if(事件名称!=null)
           事件名称(this, new EventArgs());//如果使用你自己的事件参数类,你可以用你的参数类事例替换new EventArgs(), 同时在你的参数类中保存你需要传递的数据。4、事件注册:
      事件注册和普通的事件注册没有不同,也就是说如果一个外部的对象在你的事件被触发的时候需要作出响应,那么你可以在外部了构造器中(或者适当的地方)对事件进行注册
        带有事件的类实例.事件名称+= new 事件名称EventHandler( 事件处理方法名称);5、编写事件处理方法:
      public void 事件处理方法名称(object sender, EventArgs e)
     {
         //添加你的代码
      }注:如果你在类中处理自己的触发事件,你可以选择步骤4和5的方式,也就是注册自己,也可以在触发事件代码中直接调用事件处理方法。(可能就是你所得主动调用吧,:) )
      

  2.   

    程序中的事件定义过程
    1.定义委托 —— 事件需要的委托
    注意:事件中的委托必须含有sender和Args这两个参数,其中Args参数需要从EventArgs继承。
    public delegate void EventDelegate(object sender,EventArgs e)
    2.使用委托定义事件
    public Event EventDelegate OnEventHappen;
    3.在相应方法中触发事件
    注:在类常定义protected OnEventName(EventArgs)方法来引发事件。
    相应条件if(OnEventHappen !=null ) //当注册的处理事件的方法不为空时执行
    OnEventHappen(this,new EventArgs(…))//调用事件
    …该事件会自动调用它目前引用的任何方法
    4.在别的类中使用相应的委托
    className.EventName += new EventNameHandle(MethodName)
    其中:MethodName的定义附合委托的定义一般把事件处理程序声明为proteted。事件处理程序通常是通过事件调用的,换言之,是运过委托调用的。它们不能直接调用,外部的类也看不到它们(注意外部的类看不到方法,如果委托有对该方法的引用,这些类仍可以通过委托调用该方法)。
      

  3.   

    http://www.csdn.net/develop/Read_Article.asp?Id=22951