当前位置:  编程技术>.net/c#/asp.net

基于TCP异步Socket模型的介绍

    来源: 互联网  发布时间:2014-10-18

    本文导语:  TCP异步Socket模型C#的TCP异步Socket模型是通过Begin-End模式实现的。例如提供BeginConnect、BeginAccept、 BeginSend 和 BeginReceive等。 代码如下:IAsyncResult BeginAccept(AsyncCallback callback, object state);AsyncCallback回调在函数执行完毕后执行。state对象...

TCP异步Socket模型
C#的TCP异步Socket模型是通过Begin-End模式实现的。例如提供BeginConnect、BeginAccept、 BeginSend 和 BeginReceive等。

代码如下:

IAsyncResult BeginAccept(AsyncCallback callback, object state);

AsyncCallback回调在函数执行完毕后执行。state对象被用于在执行函数和回调函数间传输信息。
代码如下:

Socket socket = new Socket(
                  AddressFamily.InterNetwork,
                  SocketType.Stream,
                  ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 8888);
socket.Bind(iep);
socket.Listen(5);
socket.BeginAccept (new AsyncCallback(CallbackAccept), socket);

private void CallbackAccept(IAsyncResult iar)
{
  Socket server = (Socket)iar.AsyncState;
  Socket client = server.EndAccept(iar);
}


则在Accept一个TcpClient,需要维护TcpClient列表。
代码如下:

private List clients;

异步TCP服务器完整实现
代码如下:

///
   /// 异步TCP服务器
   ///
   public class AsyncTcpServer : IDisposable
   {
     #region Fields

     private TcpListener listener;
     private List clients;
     private bool disposed = false;

     #endregion

     #region Ctors

     ///
     /// 异步TCP服务器
     ///
     /// 监听的端口
     public AsyncTcpServer(int listenPort)
       : this(IPAddress.Any, listenPort)
     {
     }

     ///
     /// 异步TCP服务器
     ///
     /// 监听的终结点
     public AsyncTcpServer(IPEndPoint localEP)
       : this(localEP.Address, localEP.Port)
     {
     }

     ///
     /// 异步TCP服务器
     ///
     /// 监听的IP地址
     /// 监听的端口
     public AsyncTcpServer(IPAddress localIPAddress, int listenPort)
     {
       Address = localIPAddress;
       Port = listenPort;
       this.Encoding = Encoding.Default;

       clients = new List();

       listener = new TcpListener(Address, Port);
       listener.AllowNatTraversal(true);
     }

     #endregion

     #region Properties

     ///
     /// 服务器是否正在运行
     ///
     public bool IsRunning { get; private set; }
     ///
     /// 监听的IP地址
     ///
     public IPAddress Address { get; private set; }
     ///
     /// 监听的端口
     ///
     public int Port { get; private set; }
     ///
     /// 通信使用的编码
     ///
     public Encoding Encoding { get; set; }

     #endregion

     #region Server

     ///
     /// 启动服务器
     ///
     /// 异步TCP服务器
     public AsyncTcpServer Start()
     {
       if (!IsRunning)
       {
         IsRunning = true;
         listener.Start();
         listener.BeginAcceptTcpClient(
           new AsyncCallback(HandleTcpClientAccepted), listener);
       }
       return this;
     }

     ///
     /// 启动服务器
     ///
     ///
     /// 服务器所允许的挂起连接序列的最大长度
     ///
     /// 异步TCP服务器
     public AsyncTcpServer Start(int backlog)
     {
       if (!IsRunning)
       {
         IsRunning = true;
         listener.Start(backlog);
         listener.BeginAcceptTcpClient(
           new AsyncCallback(HandleTcpClientAccepted), listener);
       }
       return this;
     }

     ///
     /// 停止服务器
     ///
     /// 异步TCP服务器
     public AsyncTcpServer Stop()
     {
       if (IsRunning)
       {
         IsRunning = false;
         listener.Stop();

         lock (this.clients)
         {
           for (int i = 0; i < this.clients.Count; i++)
           {
             this.clients[i].TcpClient.Client.Disconnect(false);
           }
           this.clients.Clear();
         }

       }
       return this;
     }

     #endregion

     #region Receive

     private void HandleTcpClientAccepted(IAsyncResult ar)
     {
       if (IsRunning)
       {
         TcpListener tcpListener = (TcpListener)ar.AsyncState;

         TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
         byte[] buffer = new byte[tcpClient.ReceiveBufferSize];

         TcpClientState internalClient
           = new TcpClientState(tcpClient, buffer);
         lock (this.clients)
         {
           this.clients.Add(internalClient);
           RaiseClientConnected(tcpClient);
         }

         NetworkStream networkStream = internalClient.NetworkStream;
         networkStream.BeginRead(
           internalClient.Buffer,
           0,
           internalClient.Buffer.Length,
           HandleDatagramReceived,
           internalClient);

         tcpListener.BeginAcceptTcpClient(
           new AsyncCallback(HandleTcpClientAccepted), ar.AsyncState);
       }
     }

     private void HandleDatagramReceived(IAsyncResult ar)
     {
       if (IsRunning)
       {
         TcpClientState internalClient = (TcpClientState)ar.AsyncState;
         NetworkStream networkStream = internalClient.NetworkStream;

         int numberOfReadBytes = 0;
         try
         {
           numberOfReadBytes = networkStream.EndRead(ar);
         }
         catch
         {
           numberOfReadBytes = 0;
         }

         if (numberOfReadBytes == 0)
         {
           // connection has been closed
           lock (this.clients)
           {
             this.clients.Remove(internalClient);
             RaiseClientDisconnected(internalClient.TcpClient);
             return;
           }
         }

         // received byte and trigger event notification
         byte[] receivedBytes = new byte[numberOfReadBytes];
         Buffer.BlockCopy(
           internalClient.Buffer, 0,
           receivedBytes, 0, numberOfReadBytes);
         RaiseDatagramReceived(internalClient.TcpClient, receivedBytes);
         RaisePlaintextReceived(internalClient.TcpClient, receivedBytes);

         // continue listening for tcp datagram packets
         networkStream.BeginRead(
           internalClient.Buffer,
           0,
           internalClient.Buffer.Length,
           HandleDatagramReceived,
           internalClient);
       }
     }

     #endregion

     #region Events

     ///
     /// 接收到数据报文事件
     ///
     public event EventHandler DatagramReceived;
     ///
     /// 接收到数据报文明文事件
     ///
     public event EventHandler PlaintextReceived;

     private void RaiseDatagramReceived(TcpClient sender, byte[] datagram)
     {
       if (DatagramReceived != null)
       {
         DatagramReceived(this, new TcpDatagramReceivedEventArgs(sender, datagram));
       }
     }

     private void RaisePlaintextReceived(TcpClient sender, byte[] datagram)
     {
       if (PlaintextReceived != null)
       {
         PlaintextReceived(this, new TcpDatagramReceivedEventArgs(
           sender, this.Encoding.GetString(datagram, 0, datagram.Length)));
       }
     }

     ///
     /// 与客户端的连接已建立事件
     ///
     public event EventHandler ClientConnected;
     ///
     /// 与客户端的连接已断开事件
     ///
     public event EventHandler ClientDisconnected;

     private void RaiseClientConnected(TcpClient tcpClient)
     {
       if (ClientConnected != null)
       {
         ClientConnected(this, new TcpClientConnectedEventArgs(tcpClient));
       }
     }

     private void RaiseClientDisconnected(TcpClient tcpClient)
     {
       if (ClientDisconnected != null)
       {
         ClientDisconnected(this, new TcpClientDisconnectedEventArgs(tcpClient));
       }
     }

     #endregion

     #region Send

     ///
     /// 发送报文至指定的客户端
     ///
     /// 客户端
     /// 报文
     public void Send(TcpClient tcpClient, byte[] datagram)
     {
       if (!IsRunning)
         throw new InvalidProgramException("This TCP server has not been started.");

       if (tcpClient == null)
         throw new ArgumentNullException("tcpClient");

       if (datagram == null)
         throw new ArgumentNullException("datagram");

       tcpClient.GetStream().BeginWrite(
         datagram, 0, datagram.Length, HandleDatagramWritten, tcpClient);
     }

     private void HandleDatagramWritten(IAsyncResult ar)
     {
       ((TcpClient)ar.AsyncState).GetStream().EndWrite(ar);
     }

     ///
     /// 发送报文至指定的客户端
     ///
     /// 客户端
     /// 报文
     public void Send(TcpClient tcpClient, string datagram)
     {
       Send(tcpClient, this.Encoding.GetBytes(datagram));
     }

     ///
     /// 发送报文至所有客户端
     ///
     /// 报文
     public void SendAll(byte[] datagram)
     {
       if (!IsRunning)
         throw new InvalidProgramException("This TCP server has not been started.");

       for (int i = 0; i < this.clients.Count; i++)
       {
         Send(this.clients[i].TcpClient, datagram);
       }
     }

     ///
     /// 发送报文至所有客户端
     ///
     /// 报文
     public void SendAll(string datagram)
     {
       if (!IsRunning)
         throw new InvalidProgramException("This TCP server has not been started.");

       SendAll(this.Encoding.GetBytes(datagram));
     }

     #endregion

     #region IDisposable Members

     ///
     /// Performs application-defined tasks associated with freeing,
     /// releasing, or resetting unmanaged resources.
     ///
     public void Dispose()
     {
       Dispose(true);
       GC.SuppressFinalize(this);
     }

     ///
     /// Releases unmanaged and - optionally - managed resources
     ///
     /// true to release
     /// both managed and unmanaged resources; false
     /// to release only unmanaged resources.
     protected virtual void Dispose(bool disposing)
     {
       if (!this.disposed)
       {
         if (disposing)
         {
           try
           {
             Stop();

             if (listener != null)
             {
               listener = null;
             }
           }
           catch (SocketException ex)
           {
             ExceptionHandler.Handle(ex);
           }
         }

         disposed = true;
       }
     }

     #endregion
   }

使用举例
代码如下:

class Program
   {
     static AsyncTcpServer server;

     static void Main(string[] args)
     {
       LogFactory.Assign(new ConsoleLogFactory());

       server = new AsyncTcpServer(9999);
       server.Encoding = Encoding.UTF8;
       server.ClientConnected +=
         new EventHandler(server_ClientConnected);
       server.ClientDisconnected +=
         new EventHandler(server_ClientDisconnected);
       server.PlaintextReceived +=
         new EventHandler(server_PlaintextReceived);
       server.Start();

       Console.WriteLine("TCP server has been started.");
       Console.WriteLine("Type something to send to client...");
       while (true)
       {
         string text = Console.ReadLine();
         server.SendAll(text);
       }
     }

     static void server_ClientConnected(object sender, TcpClientConnectedEventArgs e)
     {
       Logger.Debug(string.Format(CultureInfo.InvariantCulture,
         "TCP client {0} has connected.",
         e.TcpClient.Client.RemoteEndPoint.ToString()));
     }

     static void server_ClientDisconnected(object sender, TcpClientDisconnectedEventArgs e)
     {
       Logger.Debug(string.Format(CultureInfo.InvariantCulture,
         "TCP client {0} has disconnected.",
         e.TcpClient.Client.RemoteEndPoint.ToString()));
     }

     static void server_PlaintextReceived(object sender, TcpDatagramReceivedEventArgs e)
     {
       if (e.Datagram != "Received")
       {
         Console.Write(string.Format("Client : {0} --> ",
           e.TcpClient.Client.RemoteEndPoint.ToString()));
         Console.WriteLine(string.Format("{0}", e.Datagram));
         server.Send(e.TcpClient, "Server has received you text : " + e.Datagram);
       }
     }
   }

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 知道tcp/ip有什莫用?有socket不就行了?
  • 高分求救:SOL_SOCKET,SO_RCVTIMO,SO_SNDTIMEO,IPPOTO_TCP,TCP_NODELAY这些是在哪个头文件定义的呢?
  • C++ socket 变成遇到无法解决的tcp 分片问题
  • linux下tcp协议的socket可靠传输
  • socket tcp 关于 alarm() 和 recv() ?
  • 内核是不会把tcp/udp报文传递给raw socket??
  • udp socket和tcp的有什么冲突吗?
  • .net/c#/asp.net iis7站长之家
  • 在linux下,用socket中的tcp连接的最大数是多少?
  • socket中,只有TCP方式才有send方法吗?
  • Java里基于TCP/IP的Socket通信中一帧数据最好是多大(内详)
  • 在linux下面,为什么TCP服务器端在客户端没有断开前推出程序会关不掉socket?
  • 一个关于errno,多线程和socket(tcp/ip)的问题!
  • 用socket编程中,怎样及时的知道非正常断开的TCP连接!!
  • 200分!!嵌入式Linux下通过TCP访问服务器,Socket被服务器重置,错误号104??(高手请进)解决后加送100分!!!
  • 实现了基于TCP的Java Socket编程实例代码
  • 求linux下socket编程(tcp)实现文件传送的源程序
  • linux下socket tcp客户端读取数据
  • tcp socket客户端和服务端示例分享
  • vc++实现的tcp socket客户端和服务端示例
  • java命名空间java.net接口socketoptions的类成员方法: tcp_nodelay定义及介绍
  • http走的是tcp还是udp?ssh是tcp还是udp?
  • Linux内核中影响tcp三次握手的一些协议配置
  • 100分求《嵌入式系统Web服务器—TCP/IP Lean》或《TCP/IP Lean Web Servers for Embedded Systems 》
  • TCP协议报头字段详解及报头图
  • tcp和udp套接字有何不同?在什么情况下用tcp什么情况下用udp????
  • 对TCP协议状态及状态转换理解
  • tcp_prequeue_process怎么调用了tcp_v4_do_rcv的??求解??在tcp_recvmsg调用的
  • TCP协议四次断连过程介绍及Linux内核协议栈中相关设置项
  • 请问怎么知道每个TCP连接所属哪个进程??还有怎么样断开系统中任意一个TCP连接?
  • TCP协议三次握手过程详解


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3