2012-01-16 16 views
0

我在網上找到了這門課,但沒有任何示例用法。我試着讀它,<events>令我困惑。當我嘗試連irc.DataRead+=new EventHandler<DataReadEventArgs>(irc_DataRead);此課程的用途示例

using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading; 

namespace asynchronous 
{ 

    /// <summary> 
    /// Represents an asynchronous Tcp Client. 
    /// </summary> 
    public class Client 
    { 
     /// <summary> 
     /// The default length for the read buffer. 
     /// </summary> 
     private const int DefaultClientReadBufferLength = 4096; 

     /// <summary> 
     /// The tcp client used for the outgoing connection. 
     /// </summary> 
     private readonly TcpClient client; 

     /// <summary> 
     /// The port to connect to on the remote server. 
     /// </summary> 
     private readonly int port; 

     /// <summary> 
     /// A reset event for use if a DNS lookup is required. 
     /// </summary> 
     private readonly ManualResetEvent dnsGetHostAddressesResetEvent = null; 

     /// <summary> 
     /// The length of the read buffer. 
     /// </summary> 
     private readonly int clientReadBufferLength; 

     /// <summary> 
     /// The addresses to try connection to. 
     /// </summary> 
     private IPAddress[] addresses; 

     /// <summary> 
     /// How many times to retry connection. 
     /// </summary> 
     private int retries; 

     /// <summary> 
     /// Occurs when the client connects to the server. 
     /// </summary> 
     public event EventHandler Connected; 

     /// <summary> 
     /// Occurs when the client disconnects from the server. 
     /// </summary> 
     public event EventHandler Disconnected; 

     /// <summary> 
     /// Occurs when data is read by the client. 
     /// </summary> 
     public event EventHandler<DataReadEventArgs> DataRead; 

     /// <summary> 
     /// Occurs when data is written by the client. 
     /// </summary> 
     public event EventHandler<DataWrittenEventArgs> DataWritten; 

     /// <summary> 
     /// Occurs when an exception is thrown during connection. 
     /// </summary> 
     public event EventHandler<ExceptionEventArgs> ClientConnectException; 

     /// <summary> 
     /// Occurs when an exception is thrown while reading data. 
     /// </summary> 
     public event EventHandler<ExceptionEventArgs> ClientReadException; 

     /// <summary> 
     /// Occurs when an exception is thrown while writing data. 
     /// </summary> 
     public event EventHandler<ExceptionEventArgs> ClientWriteException; 

     /// <summary> 
     /// Occurs when an exception is thrown while performing the DNS lookup. 
     /// </summary> 
     public event EventHandler<ExceptionEventArgs> DnsGetHostAddressesException; 

     /// <summary> 
     /// Constructor for a new client object based on a host name or server address string and a port. 
     /// </summary> 
     /// <param name="hostNameOrAddress">The host name or address of the server as a string.</param> 
     /// <param name="port">The port on the server to connect to.</param> 
     /// <param name="clientReadBufferLength">The clients read buffer length.</param> 
     public Client(string hostNameOrAddress, int port, int clientReadBufferLength = DefaultClientReadBufferLength) 
      : this(port, clientReadBufferLength) 
     { 
      this.dnsGetHostAddressesResetEvent = new ManualResetEvent(false); 
      Dns.BeginGetHostAddresses(hostNameOrAddress, this.DnsGetHostAddressesCallback, null); 
     } 

     /// <summary> 
     /// Constructor for a new client object based on a number of IP Addresses and a port. 
     /// </summary> 
     /// <param name="addresses">The IP Addresses to try connecting to.</param> 
     /// <param name="port">The port on the server to connect to.</param> 
     /// <param name="clientReadBufferLength">The clients read buffer length.</param> 
     public Client(IPAddress[] addresses, int port, int clientReadBufferLength = DefaultClientReadBufferLength) 
      : this(port, clientReadBufferLength) 
     { 
      this.addresses = addresses; 
     } 

     /// <summary> 
     /// Constructor for a new client object based on a single IP Address and a port. 
     /// </summary> 
     /// <param name="address">The IP Address to try connecting to.</param> 
     /// <param name="port">The port on the server to connect to.</param> 
     /// <param name="clientReadBufferLength">The clients read buffer length.</param> 
     public Client(IPAddress address, int port, int clientReadBufferLength = DefaultClientReadBufferLength) 
      : this(new[] { address }, port, clientReadBufferLength) 
     { 
     } 

     /// <summary> 
     /// Private constructor for a new client object. 
     /// </summary> 
     /// <param name="port">The port on the server to connect to.</param> 
     /// <param name="clientReadBufferLength">The clients read buffer length.</param> 
     private Client(int port, int clientReadBufferLength) 
     { 
      this.client = new TcpClient(); 
      this.port = port; 
      this.clientReadBufferLength = clientReadBufferLength; 
     } 

     /// <summary> 
     /// Starts an asynchronous connection to the remote server. 
     /// </summary> 
     public void Connect() 
     { 
      if (this.dnsGetHostAddressesResetEvent != null) 
       this.dnsGetHostAddressesResetEvent.WaitOne(); 
      this.retries = 0; 
      this.client.BeginConnect(this.addresses, this.port, this.ClientConnectCallback, null); 
     } 

     /// <summary> 
     /// Writes a string to the server using a given encoding. 
     /// </summary> 
     /// <param name="value">The string to write.</param> 
     /// <param name="encoding">The encoding to use.</param> 
     /// <returns>A Guid that can be used to match the data written to the confirmation event.</returns> 
     public Guid Write(string value, Encoding encoding) 
     { 
      byte[] buffer = encoding.GetBytes(value); 
      return this.Write(buffer); 
     } 

     /// <summary> 
     /// Writes a byte array to the server. 
     /// </summary> 
     /// <param name="buffer">The byte array to write.</param> 
     /// <returns>A Guid that can be used to match the data written to the confirmation event.</returns> 
     public Guid Write(byte[] buffer) 
     { 
      Guid guid = Guid.NewGuid(); 
      NetworkStream networkStream = this.client.GetStream(); 
      networkStream.BeginWrite(buffer, 0, buffer.Length, this.ClientWriteCallback, guid); 
      return guid; 
     } 

     /// <summary> 
     /// Callback from the asynchronous DNS lookup. 
     /// </summary> 
     /// <param name="asyncResult">The result of the async operation.</param> 
     private void DnsGetHostAddressesCallback(IAsyncResult asyncResult) 
     { 
      try 
      { 
       this.addresses = Dns.EndGetHostAddresses(asyncResult); 
       this.dnsGetHostAddressesResetEvent.Set(); 
      } 
      catch (Exception ex) 
      { 
       if (this.DnsGetHostAddressesException != null) 
        this.DnsGetHostAddressesException(this, new ExceptionEventArgs(ex)); 
      } 
     } 

     /// <summary> 
     /// Callback from the asynchronous Connect method. 
     /// </summary> 
     /// <param name="asyncResult">The result of the async operation.</param> 
     private void ClientConnectCallback(IAsyncResult asyncResult) 
     { 
      try 
      { 
       this.client.EndConnect(asyncResult); 
       if (this.Connected != null) 
        this.Connected(this, new EventArgs()); 
      } 
      catch (Exception ex) 
      { 
       retries++; 
       if (retries < 3) 
       { 
        this.client.BeginConnect(this.addresses, this.port, this.ClientConnectCallback, null); 
       } 
       else 
       { 
        if (this.ClientConnectException != null) 
         this.ClientConnectException(this, new ExceptionEventArgs(ex)); 
       } 
       return; 
      } 

      try 
      { 
       NetworkStream networkStream = this.client.GetStream(); 
       byte[] buffer = new byte[this.clientReadBufferLength]; 
       networkStream.BeginRead(buffer, 0, buffer.Length, this.ClientReadCallback, buffer); 
      } 
      catch (Exception ex) 
      { 
       if (this.ClientReadException != null) 
        this.ClientReadException(this, new ExceptionEventArgs(ex)); 
      } 
     } 

     /// <summary> 
     /// Callback from the asynchronous Read method. 
     /// </summary> 
     /// <param name="asyncResult">The result of the async operation.</param> 
     private void ClientReadCallback(IAsyncResult asyncResult) 
     { 
      try 
      { 
       NetworkStream networkStream = this.client.GetStream(); 
       int read = networkStream.EndRead(asyncResult); 

       if (read == 0) 
       { 
        if (this.Disconnected != null) 
         this.Disconnected(this, new EventArgs()); 
       } 

       byte[] buffer = asyncResult.AsyncState as byte[]; 
       if (buffer != null) 
       { 
        byte[] data = new byte[read]; 
        Buffer.BlockCopy(buffer, 0, data, 0, read); 
        networkStream.BeginRead(buffer, 0, buffer.Length, this.ClientReadCallback, buffer); 
        if (this.DataRead != null) 
         this.DataRead(this, new DataReadEventArgs(data)); 
       } 
      } 
      catch (Exception ex) 
      { 
       if (this.ClientReadException != null) 
        this.ClientReadException(this, new ExceptionEventArgs(ex)); 
      } 
     } 

     /// <summary> 
     /// Callback from the asynchronous write callback. 
     /// </summary> 
     /// <param name="asyncResult">The result of the async operation.</param> 
     private void ClientWriteCallback(IAsyncResult asyncResult) 
     { 
      try 
      { 
       NetworkStream networkStream = this.client.GetStream(); 
       networkStream.EndWrite(asyncResult); 
       Guid guid = (Guid)asyncResult.AsyncState; 
       if (this.DataWritten != null) 
        this.DataWritten(this, new DataWrittenEventArgs(guid)); 
      } 
      catch (Exception ex) 
      { 
       if (this.ClientWriteException != null) 
        this.ClientWriteException(this, new ExceptionEventArgs(ex)); 
      } 
     } 
    } 

    /// <summary> 
    /// Provides data for an exception occuring event. 
    /// </summary> 
    public class ExceptionEventArgs : EventArgs 
    { 
     /// <summary> 
     /// Constructor for a new Exception Event Args object. 
     /// </summary> 
     /// <param name="ex">The exception that was thrown.</param> 
     public ExceptionEventArgs(Exception ex) 
     { 
      this.Exception = ex; 
     } 

     public Exception Exception { get; private set; } 
    } 

    /// <summary> 
    /// Provides data for a data read event. 
    /// </summary> 
    public class DataReadEventArgs : EventArgs 
    { 
     /// <summary> 
     /// Constructor for a new Data Read Event Args object. 
     /// </summary> 
     /// <param name="data">The data that was read from the remote host.</param> 
     public DataReadEventArgs(byte[] data) 
     { 
      this.Data = data; 
     } 

     /// <summary> 
     /// Gets the data that has been read. 
     /// </summary> 
     public byte[] Data { get; private set; } 
    } 

    /// <summary> 
    /// Provides data for a data write event. 
    /// </summary> 
    public class DataWrittenEventArgs : EventArgs 
    { 
     /// <summary> 
     /// Constructor for a Data Written Event Args object. 
     /// </summary> 
     /// <param name="guid">The guid of the data written.</param> 
     public DataWrittenEventArgs(Guid guid) 
     { 
      this.Guid = guid; 
     } 

     /// <summary> 
     /// Gets the Guid used to match the data written to the confirmation event. 
     /// </summary> 
     public Guid Guid { get; private set; } 
    } 
} 

後,初始化它保持null事件這是我做過嘗試,但我不明白這一點。我是新來的C#:

using System; 
using System.Text; 
using System.Threading; 

namespace asynchronous 
{ 
    class Program 
    { 
     private static EventHandler connection; 
     private static EventHandler<DataReadEventArgs> irc_DataRead; 
     static void Main(string[] args) 
     { 
      var irc = new Client("irc.rizon.net", 6667); 
      Console.WriteLine("Connecting..."); 
      irc.Connect(); 
      Thread.Sleep(2000); 
      irc.Write("Test", Encoding.UTF8); 
      irc.DataRead+=new EventHandler<DataReadEventArgs>(irc_DataRead); 
      Console.WriteLine(irc_DataRead); 
      Console.WriteLine("Connected."); 
      Console.ReadLine(); 
     } 
    } 
} 

有人可以請幫助我設置該類連接到IRC和讀寫文本?

+0

你見過? http://www.meebey.net/projects/smartirc4net/ – 2012-01-16 22:23:33

+0

我沒有。你知道如果smartirc4net是異步的嗎?我需要一個異步連接,因爲這個頻道有很多同伴。 – Ryan 2012-01-16 22:26:13

+0

RFC 2812指出'服務器和客戶端之間互相發送消息,這些消息可能會或可能不會產生回覆',因此建議IRC本質上是異步的。因此,這是一個很好的機會。 – 2012-01-16 23:30:45

回答

1

在這裏,您將一個事件處理程序的實例連接到一個事件中。你需要連接一個實際的方法/ lambda以使其工作。請嘗試以下方法

static void OnDataRead(object sender, DataReadEventArgs e) { 
    // Data reads call this method 
} 


static void Main(string[] args) { 
    ... 
    irc.DataRead += OnDataRead; 
    ... 
} 
+0

謝謝Jared :) – Ryan 2012-01-16 22:46:07