一、摘要
本篇阐述基于TCP通信协议的异步实现。
二、实验平台
Visual Studio 2010
三、异步通信实现原理及常用方法
3.1 建立连接
在同步模式中,在服务器上使用Accept方法接入连接请求,而在客户端则使用Connect方法来连接服务器。相对地,在异步模式下,服务器可以使用BeginAccept方法和EndAccept方法来完成连接到客户端的任务,在客户端则通过BeginConnect方法和EndConnect方法来实现与服务器的连接。
BeginAccept在异步方式下传入的连接尝试,它允许其他动作而不必等待连接建立才继续执行后面程序。在调用BeginAccept之前,必须使用Listen方法来侦听是否有连接请求,BeginAccept的函数原型为:
BeginAccept(AsyncCallback AsyncCallback, Ojbect state)</div>
参数:
AsyncCallBack:代表回调函数
state:表示状态信息,必须保证state中包含socket的句柄
使用BeginAccept的基本流程是:
(1)创建本地终节点,并新建套接字与本地终节点进行绑定;
(2)在端口上侦听是否有新的连接请求;
(3)请求开始接入新的连接,传入Socket的实例或者StateOjbect的实例。
参考代码:
//定义IP地址 IPAddress local = IPAddress.Parse("127.0,0,1"); IPEndPoint iep = new IPEndPoint(local,13000); //创建服务器的socket对象 Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); server.Bind(iep); server.Listen(20); server.BeginAccecpt(new AsyncCallback(Accept),server);</div>
当BeginAccept()方法调用结束后,一旦新的连接发生,将调用回调函数,而该回调函数必须包括用来结束接入连接操作的EndAccept()方法。
该方法参数列表为 Socket EndAccept(IAsyncResult iar)
下面为回调函数的实例:
void Accept(IAsyncResult iar) { //还原传入的原始套接字 Socket MyServer = (Socket)iar.AsyncState; //在原始套接字上调用EndAccept方法,返回新的套接字 Socket service = MyServer.EndAccept(iar); }</div>
至此,服务器端已经准备好了。客户端应通过BeginConnect方法和EndConnect来远程连接主机。在调用BeginConnect方法时必须注册相应的回调函数并且至少传递一个Socket的实例给state参数,以保证EndConnect方法中能使用原始的套接字。下面是一段是BeginConnect的调用:
Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp) IPAddress ip=IPAddress.Parse("127.0.0.1"); IPEndPoint iep=new IPEndPoint(ip,13000); socket.BeginConnect(iep, new AsyncCallback(Connect),socket);</div>
EndConnect是一种阻塞方法,用于完成BeginConnect方法的异步连接诶远程主机的请求。在注册了回调函数后必须接收BeginConnect方法返回的IASynccReuslt作为参数。下面为代码演示:
void Connect(IAsyncResult iar) { Socket client=(Socket)iar.AsyncState; try { client.EndConnect(iar); } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { } }</div>
除了采用上述方法建立连接之后,也可以采用TcpListener类里面的方法进行连接建立。下面是服务器端对关于TcpListener类使用BeginAccetpTcpClient方法处理一个传入的连接尝试。以下是使用BeginAccetpTcpClient方法和EndAccetpTcpClient方法的代码:
public static void DoBeginAccept(TcpListener listner) { //开始从客户端监听连接 Console.WriteLine("Waitting for a connection"); //接收连接 //开始准备接入新的连接,一旦有新连接尝试则调用回调函数DoAcceptTcpCliet listner.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpCliet), listner); } //处理客户端的连接 public static void DoAcceptTcpCliet(IAsyncResult iar) { //还原原始的TcpListner对象 TcpListener listener = (TcpListener)iar.AsyncState; //完成连接的动作,并返回新的TcpClient TcpClient client = listener.EndAcceptTcpClient(iar); Console.WriteLine("连接成功"); }</div>
代码的处理逻辑为:
(1)调用BeginAccetpTcpClient方法开开始连接新的连接,当连接视图发生时,回调函数被调用以完成连接操作;
(2)上面DoAcceptTcpCliet方法通过AsyncState属性获得由BeginAcceptTcpClient传入的listner实例;
(3)在得到listener对象后,用它调用EndAcceptTcpClient方法,该方法返回新的包含客户端信息的TcpClient。
BeginConnect方法和EndConnect方法可用于客户端尝试建立与服务端的连接,这里和第一种方法并无区别。下面看实例:
public void doBeginConnect(IAsyncResult iar) { Socket client=(Socket)iar.AsyncState; //开始与远程主机进行连接 client.BeginConnect(serverIP[0],13000,requestCallBack,client); Console.WriteLine("开始与服务器进行连接"); } private void requestCallBack(IAsyncResult iar) { try { //还原原始的TcpClient对象 TcpClient client=(TcpClient)iar.AsyncState; // client.EndConnect(iar); Console.WriteLine("与服务器{0}连接成功",client.Client.RemoteEndPoint); } catch(Exception e) { Console.WriteLine(e.ToString()); } finally { } }</div>
以上是建立连接的两种方法。可根据需要选择使用。
3.2 发送与接受数据
在建立了套接字的连接后,就可以服务器端和客户端之间进行数据通信了。异步套接字用BeginSend和EndSend方法来负责数据的发送。注意在调用BeginSend方法前要确保双方都已经建立连接,否则会出异常。下面演示代码:
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()); } }</div>
接收数据是通过BeginReceive和EndReceive方法:
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()); } } 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()); } }</div>
上述代