C#使用异步客户端套接字

来源:互联网 发布:unity3d 打包ios 详细 编辑:程序博客网 时间:2024/05/21 22:48

        异步客户端套接字在等待网络操作完成时不挂起应用程序。因为它使用标准 .NET Framework 异步编程模型在一个线程上处理网络连接,而应用程序继续在原始线程上运行。异步套接字适用于大量使用网络或者不能等待网络操作完成后才能继续执行的应用程序。

        Socket类遵循异步方法的.Net Framework命名模式;例如,同步Receive方法对应异步BeginRecive和EndReceive方法。

        异步操作要求回调方法返回操作结果。如果应用程序不需要知道结果,则不需要任何回调方法。

        本节的代码示例阐释:

        1、如何使用某个方法开始与某个网络设备进行连接,并使用回调方法结束连接;

        2、如何使用某个方法开始发送数据,并使用回调方法完成数据发送;

        3、如何使用某个方法开始接受数据,并使用回调方法结束接收数据。

        异步套接字使用系统线程池中的多个线程处理网络连接。一个线程负责开始数据的发送或接收,其它线程完成与网络设备的连接并发送或接收数据。

        在示例中,System.Threading.ManualResetEvent 类的实例用于挂起主线程的执行并在执行可以继续时发出信号。

        在下面的示例中,为了将异步套接字连接到网络设备,Connect()方法初始化一个Socket,然后调用BeginConnect()方法,传递表示网络设备的远程终结点,连接回调方法,状态对象(即客户端 Socket,用于在异步调用之间传递状态信息)。

        该实例实现Connect()方法以将指定的Socket连接到指定的终结点。它采用一个名为connectDone 的ManualResetEvent 类的实例,为全局变量。

        public static void Connect(EndPoint remoteEP, Socket client)

        {
                client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client );
                connectDone.WaitOne();
        }

        连接回调方法 ConnectCallback 实现 AsyncCallback 委托。它在远程设备可用时连接到远程设备,然后通过设置ManualResetEvent 的实例connectDone 向应用程序线程发出连接完成的信号。下面的代码实现ConnectCallback 方法。

        private static void ConnectCallback(IAsyncResult ar)    

        {
              try

              {
                   // Retrieve the socket from the state object.
                   Socket client = (Socket) ar.AsyncState;
                   // Complete the connection.
                   client.EndConnect(ar);
                   Console.WriteLine("Socket connected to {0}",client.RemoteEndPoint.ToString());
                   // Signal that the connection has been made.
                   connectDone.Set();
               }

              catch (Exception e)

              {
                   Console.WriteLine(e.ToString());
              }
         }

         Send 示例方法以 ASCII 格式对指定的字符串数据进行编码,并将其异步发送到指定的套接字所表示的网络设备。

         下面的示例实现 Send 方法。

         private static void Send(Socket client, 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.
             client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None,new AsyncCallback(SendCallback), client);
         }

         发送回调方法 SendCallback 实现 AsyncCallback 委托。它在网络设备准备接收时发送数据。下面的示例显示SendCallback 方法的实现。它采用一个名为 sendDone 的全局 ManualResetEvent。

         private static void SendCallback(IAsyncResult ar)

         {
               try

               {
                     // Retrieve the socket from the state object.
                     Socket client = (Socket) ar.AsyncState;

                     // Complete sending the data to the remote device.
                     int bytesSent = client.EndSend(ar);
                     Console.WriteLine("Sent {0} bytes to server.", bytesSent);

                     // Signal that all bytes have been sent.
                     sendDone.Set();
               }

              catch (Exception e)

              {
                     Console.WriteLine(e.ToString());
              }
         }

        

         从客户端套接字读取数据需要一个在异步调用之间传递值的状态对象。下面的类是用于从客户端套接字接收数据的示例状态对象。它包含以下各项的字段:客户端套接字,用于接收数据的缓冲区,以及用于保存传入数据字符串的 StringBuilder。将这些字段放在该状态对象中,使这些字段的值在多个调用之间得以保留,以便从客户端套接字读取数据。

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

          Receive 方法示例设置状态对象,然后调用 BeginReceive 方法从客户端套接字异步读取数据。下面的示例实现Receive 方法。

         private static void Receive(Socket client)

         {
                try

                {
                      // Create the state object.
                      StateObject state = new StateObject();
                      state.workSocket = client;

                       // Begin receiving the data from the remote device.
                      client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReceiveCallback), state);
                 }

                 catch (Exception e)

                 {
                      Console.WriteLine(e.ToString());
                 }
          }

         接收回调方法 ReceiveCallback 实现 AsyncCallback 委托。它接收来自网络设备的数据并生成消息字符串。它将来自网络的一个或多个数据字节读入数据缓冲区,然后再次调用 BeginReceive 方法,直到客户端发送的数据完成为止。从客户端读取所有数据后,ReceiveCallback 通过设置 ManualResetEventsendDone 向应用程序线程发出数据完成的信号。

         下面的示例代码实现 ReceiveCallback 方法。它采用一个名为 response 的全局字符串,其中包含接收到的字符串和一个名为receiveDone 的全局 ManualResetEvent。服务器必须完全关闭客户端套接字才能结束该网络会话。

          private static void ReceiveCallback( IAsyncResult ar )

          {
                 try

                 {
                       // Retrieve the state object and the client socket 
                       // from the asynchronous state object.
                       StateObject state = (StateObject) ar.AsyncState;
                       Socket client = state.workSocket;
                       // Read data from the remote device.
                       int bytesRead = client.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));
                              // Get the rest of the data.
                             client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback), state);
                        }

                        else

                        {
                             // All the data has arrived; put it in response.
                             if (state.sb.Length > 1)

                             {
                                   response = state.sb.ToString();
                             }
                             // Signal that all bytes have been received.
                             receiveDone.Set();
                         }
                   }

                  catch (Exception e)

                  {
                         Console.WriteLine(e.ToString());
                  }
          }