欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于TCP異步Socket模型的介紹

 更新時(shí)間:2013年04月18日 10:57:11   作者:  
本篇文章小編將為大家介紹,基于TCP異步Socket模型的介紹,需要的朋友參考下

TCP異步Socket模型
C#的TCP異步Socket模型是通過(guò)Begin-End模式實(shí)現(xiàn)的。例如提供BeginConnect、BeginAccept、 BeginSend 和 BeginReceive等。

復(fù)制代碼 代碼如下:

IAsyncResult BeginAccept(AsyncCallback callback, object state);

AsyncCallback回調(diào)在函數(shù)執(zhí)行完畢后執(zhí)行。state對(duì)象被用于在執(zhí)行函數(shù)和回調(diào)函數(shù)間傳輸信息。
復(fù)制代碼 代碼如下:

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一個(gè)TcpClient,需要維護(hù)TcpClient列表。
復(fù)制代碼 代碼如下:

private List<TcpClientState> clients;

異步TCP服務(wù)器完整實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

/// <summary>
   /// 異步TCP服務(wù)器
   /// </summary>
   public class AsyncTcpServer : IDisposable
   {
     #region Fields

     private TcpListener listener;
     private List<TcpClientState> clients;
     private bool disposed = false;

     #endregion

     #region Ctors

     /// <summary>
     /// 異步TCP服務(wù)器
     /// </summary>
     /// <param name="listenPort">監(jiān)聽(tīng)的端口</param>
     public AsyncTcpServer(int listenPort)
       : this(IPAddress.Any, listenPort)
     {
     }

     /// <summary>
     /// 異步TCP服務(wù)器
     /// </summary>
     /// <param name="localEP">監(jiān)聽(tīng)的終結(jié)點(diǎn)</param>
     public AsyncTcpServer(IPEndPoint localEP)
       : this(localEP.Address, localEP.Port)
     {
     }

     /// <summary>
     /// 異步TCP服務(wù)器
     /// </summary>
     /// <param name="localIPAddress">監(jiān)聽(tīng)的IP地址</param>
     /// <param name="listenPort">監(jiān)聽(tīng)的端口</param>
     public AsyncTcpServer(IPAddress localIPAddress, int listenPort)
     {
       Address = localIPAddress;
       Port = listenPort;
       this.Encoding = Encoding.Default;

       clients = new List<TcpClientState>();

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

     #endregion

     #region Properties

     /// <summary>
     /// 服務(wù)器是否正在運(yùn)行
     /// </summary>
     public bool IsRunning { get; private set; }
     /// <summary>
     /// 監(jiān)聽(tīng)的IP地址
     /// </summary>
     public IPAddress Address { get; private set; }
     /// <summary>
     /// 監(jiān)聽(tīng)的端口
     /// </summary>
     public int Port { get; private set; }
     /// <summary>
     /// 通信使用的編碼
     /// </summary>
     public Encoding Encoding { get; set; }

     #endregion

     #region Server

     /// <summary>
     /// 啟動(dòng)服務(wù)器
     /// </summary>
     /// <returns>異步TCP服務(wù)器</returns>
     public AsyncTcpServer Start()
     {
       if (!IsRunning)
       {
         IsRunning = true;
         listener.Start();
         listener.BeginAcceptTcpClient(
           new AsyncCallback(HandleTcpClientAccepted), listener);
       }
       return this;
     }

     /// <summary>
     /// 啟動(dòng)服務(wù)器
     /// </summary>
     /// <param name="backlog">
     /// 服務(wù)器所允許的掛起連接序列的最大長(zhǎng)度
     /// </param>
     /// <returns>異步TCP服務(wù)器</returns>
     public AsyncTcpServer Start(int backlog)
     {
       if (!IsRunning)
       {
         IsRunning = true;
         listener.Start(backlog);
         listener.BeginAcceptTcpClient(
           new AsyncCallback(HandleTcpClientAccepted), listener);
       }
       return this;
     }

     /// <summary>
     /// 停止服務(wù)器
     /// </summary>
     /// <returns>異步TCP服務(wù)器</returns>
     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

     /// <summary>
     /// 接收到數(shù)據(jù)報(bào)文事件
     /// </summary>
     public event EventHandler<TcpDatagramReceivedEventArgs<byte[]>> DatagramReceived;
     /// <summary>
     /// 接收到數(shù)據(jù)報(bào)文明文事件
     /// </summary>
     public event EventHandler<TcpDatagramReceivedEventArgs<string>> PlaintextReceived;

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

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

     /// <summary>
     /// 與客戶(hù)端的連接已建立事件
     /// </summary>
     public event EventHandler<TcpClientConnectedEventArgs> ClientConnected;
     /// <summary>
     /// 與客戶(hù)端的連接已斷開(kāi)事件
     /// </summary>
     public event EventHandler<TcpClientDisconnectedEventArgs> 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

     /// <summary>
     /// 發(fā)送報(bào)文至指定的客戶(hù)端
     /// </summary>
     /// <param name="tcpClient">客戶(hù)端</param>
     /// <param name="datagram">報(bào)文</param>
     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);
     }

     /// <summary>
     /// 發(fā)送報(bào)文至指定的客戶(hù)端
     /// </summary>
     /// <param name="tcpClient">客戶(hù)端</param>
     /// <param name="datagram">報(bào)文</param>
     public void Send(TcpClient tcpClient, string datagram)
     {
       Send(tcpClient, this.Encoding.GetBytes(datagram));
     }

     /// <summary>
     /// 發(fā)送報(bào)文至所有客戶(hù)端
     /// </summary>
     /// <param name="datagram">報(bào)文</param>
     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);
       }
     }

     /// <summary>
     /// 發(fā)送報(bào)文至所有客戶(hù)端
     /// </summary>
     /// <param name="datagram">報(bào)文</param>
     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

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

     /// <summary>
     /// Releases unmanaged and - optionally - managed resources
     /// </summary>
     /// <param name="disposing"><c>true</c> to release
     /// both managed and unmanaged resources; <c>false</c>
     /// to release only unmanaged resources.</param>
     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
   }

使用舉例
復(fù)制代碼 代碼如下:

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<TcpClientConnectedEventArgs>(server_ClientConnected);
       server.ClientDisconnected +=
         new EventHandler<TcpClientDisconnectedEventArgs>(server_ClientDisconnected);
       server.PlaintextReceived +=
         new EventHandler<TcpDatagramReceivedEventArgs<string>>(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<string> 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);
       }
     }
   }

相關(guān)文章

  • C#讀取XML的CDATA節(jié)點(diǎn)內(nèi)容實(shí)例詳解

    C#讀取XML的CDATA節(jié)點(diǎn)內(nèi)容實(shí)例詳解

    在本篇文章里小編給大家整理了關(guān)于C# 讀取XML的CDATA節(jié)點(diǎn)內(nèi)容的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考學(xué)習(xí)下。
    2019-09-09
  • 解析C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用

    解析C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用

    這篇文章主要介紹了C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用,備忘錄模式用來(lái)保存與對(duì)象有關(guān)的數(shù)據(jù)用以在將來(lái)對(duì)對(duì)象進(jìn)行復(fù)原,需要的朋友可以參考下
    2016-02-02
  • C#設(shè)計(jì)模式之單例模式實(shí)例講解

    C#設(shè)計(jì)模式之單例模式實(shí)例講解

    這篇文章主要介紹了C#設(shè)計(jì)模式之單例模式實(shí)例講解,本文講解了單例模式的定義、單例模式的優(yōu)缺點(diǎn),需要的朋友可以參考下
    2014-10-10
  • C#實(shí)現(xiàn)的文件壓縮和解壓縮類(lèi)

    C#實(shí)現(xiàn)的文件壓縮和解壓縮類(lèi)

    這篇文章主要介紹了C#實(shí)現(xiàn)的文件壓縮和解壓縮類(lèi),實(shí)例分析了C#針對(duì)文件壓縮與解壓縮的常用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • C# 減少嵌套循環(huán)的兩種方法

    C# 減少嵌套循環(huán)的兩種方法

    最近在解決性能優(yōu)化的問(wèn)題,看到了一堆嵌套循環(huán),四五層級(jí)的循環(huán)真的有點(diǎn)過(guò)分了,在數(shù)據(jù)量成萬(wàn),十萬(wàn)級(jí)別的時(shí)候,真的非常影響性能。本文介紹了C# 減少嵌套循環(huán)的兩種方法,幫助各位選擇適合自己的優(yōu)化方案,優(yōu)化程序性能
    2021-06-06
  • C# WebApi CORS跨域問(wèn)題解決方案

    C# WebApi CORS跨域問(wèn)題解決方案

    本篇文章主要介紹了C# WebApi CORS跨域問(wèn)題解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的示例詳解

    C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的示例詳解

    AForge是一個(gè)專(zhuān)門(mén)為開(kāi)發(fā)者和研究者基于C#框架設(shè)計(jì)的,這個(gè)框架提供了不同的類(lèi)庫(kù)和關(guān)于類(lèi)庫(kù)的資源,本文為大家介紹了C#使用AForge實(shí)現(xiàn)調(diào)用攝像頭的相關(guān)教程,需要的可以了解下
    2023-11-11
  • 解決unity3d導(dǎo)入模型貼圖材質(zhì)丟失的問(wèn)題

    解決unity3d導(dǎo)入模型貼圖材質(zhì)丟失的問(wèn)題

    這篇文章主要介紹了解決unity3d導(dǎo)入模型貼圖材質(zhì)丟失的問(wèn)題,具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • C#如何消除驗(yàn)證碼圖片的鋸齒效果

    C#如何消除驗(yàn)證碼圖片的鋸齒效果

    這篇文章主要為大家詳細(xì)介紹了C#如何消除驗(yàn)證碼圖片的鋸齒效果,有無(wú)鋸齒主要依靠一句代碼,想要知道的朋友可閱讀下文
    2016-09-09
  • C#實(shí)現(xiàn)排序的代碼詳解

    C#實(shí)現(xiàn)排序的代碼詳解

    在本篇文章里小編給大家整理的是關(guān)于C#實(shí)現(xiàn)排序的代碼以及相關(guān)知識(shí)點(diǎn),需要的朋友們參考下。
    2019-10-10

最新評(píng)論