今天在测试Socket的异步方式的使用方法时,进一步发现了对AutoResetEvent和ManualResetEvent的进一步理解。到底是先把ManuslResetEvent的例子代码贴出来还是把Socket的例子代码贴出来呢?
我决定先把关于Socket的例子代码贴出来再说。下面的代码也是摘自MSND,在“使用.NET Framework编程”这一部分中有一章讲述的是“套接字”,其中的示例代码包含了“异步客户端套接字示例”和“异步服务器套接字示例”,代码如下:
异步服务器套接字示例:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

/// <summary>
/// 用于异步读取客户端数据的状态对象,根据文档,使用Socket通信的时候,
/// 在进行异步调用的情况下,需要一个对象
/// </summary>
public class StateObject {
    //客户端Socket
    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;
    // 线程信号,这里采用的是ManualResetEvent,就是手动的
 // 恢复非终止状态的信号对象,另外,这个程序中的线程也是
 // 隐含的,而不是明确的线程对象,定义为静态
    public static ManualResetEvent allDone = new ManualResetEvent(false);
 /// <summary>
 /// 构造函数
 /// </summary>
    public AsynchronousSocketListener()
 {
    }
 /// <summary>
 /// 注意,这里定义的也是静态的方法
 /// </summary>
    public static void StartListening() {
        // 收取数据的数据缓冲区
        byte[] bytes = new Byte[1024];
        // 为Socket创建一个本地的终端点
        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
        // 创建一个TCP/IP的socket
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp );
        // 将socket绑定到本地终端点然后侦听到来的连接,
        // 这应该是Server的特点,在客户端不存在这种问题
        try {
            listener.Bind(localEndPoint);
            listener.Listen(100);
            while (true) {
                // 将事件置为非信号状态,这里是手动设定的区别于AutoResetEvent
                allDone.Reset();
                // 开始一个异步Socket以侦听连接
                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) {
        // Signal the main thread to continue.
        allDone.Set();
        // Get the socket that handles the client request.
        Socket listener = (Socket) ar.AsyncState;
        Socket handler = listener.EndAccept(ar);
        // Create the state object.
        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;
       
        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject) ar.AsyncState;
        Socket handler = state.workSocket;
        // Read data from the client socket.
        int bytesRead = handler.EndReceive(ar);
        if (bytesRead > 0) {
            // There  might be more data, so store the data received so far.
            state.sb.Append(Encoding.ASCII.GetString(
                state.buffer,0,bytesRead));
            // Check for end-of-file tag. If it is not there, read
            // more data.
            content = state.sb.ToString();
            if (content.IndexOf("<EOF>") > -1) {
                // All the data has been read from the
                // client. Display it on the console.
                Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                    content.Length, content );
                // Echo the data back to the client.
                Send(handler, content);
            } else {
                // Not all data received. Get more.
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReadCallback), state);
            }
        }
    }
   
    private static void Send(Socket handler, String data) {
        // Convert the string data to byte data using ASCII encoding.
        byte[] byteData = Encoding.ASCII.GetBytes(data);
        // Begin sending the data to the remote device.
        handler.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), handler);
    }
    private static void SendCallback(IAsyncResult ar) {
        try {
            // Retrieve the socket from the state object.
            Socket handler = (Socket) ar.AsyncState;
            // Complete sending the data to the remote device.
            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;
    }
}
异步客户端套接字示例:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
// State object for receiving data from remote device.
public class StateObject {
    // Client socket.
    public Socket workSocket = null;
    // Size of receive buffer.
    public const int BufferSize = 256;
    // Receive buffer.
    public byte[] buffer = new byte[BufferSize];
    // Received data string.
    public StringBuilder sb = new StringBuilder();
}
public class AsynchronousClient {
    // The port number for the remote device.
    private const int port = 11000;
    // ManualResetEvent instances signal completion.
    private static ManualResetEvent connectDone =
        new ManualResetEvent(false);
    private static ManualResetEvent sendDone =
        new ManualResetEvent(false);
    private static ManualResetEvent receiveDone =
        new ManualResetEvent(false);
    // The response from the remote device.
    private static String response = String.Empty;
    private static void StartClient() {
        // Connect to a remote device.
        try {
            // Establish the remote endpoint for the socket.
            // The name of the
            // remote device is "host.contoso.com".
            IPHostEntry ipHostInfo = Dns.Resolve("LiuyiNoteBook");
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
            // Create a TCP/IP socket.
            Socket client = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
            // Connect to the remote endpoint.
            client.BeginConnect( remoteEP,
                new AsyncCallback(ConnectCallback), client);
            connectDone.WaitOne();
            // Send test data to the remote device.