C#網路編程系列文章(七)之UdpClient實現非同步UDP伺服器

來源:互聯網
上載者:User

本文介紹

UdpClient 類在同步阻塞模式中為發送和接收不需連線的 UDP 資料包而提供了簡單的方法。因為 UDP 是一種不需連線的傳輸協議,所以你不需要在發送和接收資料之前建立任何遠程主機串連。你只需要按照下列方式來建立預設的遠程主機選項:
使用遠程主機名稱和連接埠號碼作為參數來建立 UdpClient 類的執行個體。
建立 UdpClient 類的執行個體然後調用 Connect 方法。
你可以使用任何由 UdpClient 所提供的發送方法把資料發送給遠程裝置。然後使用 Receive 方法來接收來自於遠程主機的資料。
提示:如果你已經指定了一個預設的遠程主機,就不要使用主機名稱或者 IPEndPoint 來調用 Send 方法。如果你這樣做,那麼 UdpClient 將會拋出一個異常。
UdpClient 方法同樣允許你發送和接收多點傳送的資料包。而使用 JoinMulticastGroup 方法可以把 UdpClient 訂閱到多點傳送分組。也可以使用 DropMulticastGroup 方法把 UdpClient 從多點傳送分組的訂閱中取消。

本節介紹使用UdpClient實現一個非同步高效能UDP伺服器

UdpClient非同步UDP伺服器


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.Net;namespace NetFrame.Net.UDP.Listener.Asynchronous{    /// <summary>    /// UdpClient 實現非同步UDP伺服器    /// </summary>    public class AsyncUDPServer    {        #region Fields        /// <summary>        /// 伺服器程式允許的最大用戶端串連數        /// </summary>        private int _maxClient;        /// <summary>        /// 當前的串連的用戶端數        /// </summary>        //private int _clientCount;        /// <summary>        /// 伺服器使用的非同步UdpClient        /// </summary>        private UdpClient _server;        /// <summary>        /// 用戶端工作階段列表        /// </summary>        //private List<AsyncUDPState> _clients;        private bool disposed = false;        /// <summary>        /// 資料接受緩衝區        /// </summary>        private byte[] _recvBuffer;        #endregion        #region Properties        /// <summary>        /// 伺服器是否正在運行        /// </summary>        public bool IsRunning { get; private set; }        /// <summary>        /// 監聽的IP地址        /// </summary>        public IPAddress Address { get; private set; }        /// <summary>        /// 監聽的連接埠        /// </summary>        public int Port { get; private set; }        /// <summary>        /// 通訊使用的編碼        /// </summary>        public Encoding Encoding { get; set; }        #endregion        #region 建構函式        /// <summary>        /// 非同步UdpClient UDP伺服器        /// </summary>        /// <param name="listenPort">監聽的連接埠</param>        public AsyncUDPServer(int listenPort)            : this(IPAddress.Any, listenPort,1024)        {        }        /// <summary>        /// 非同步UdpClient UDP伺服器        /// </summary>        /// <param name="localEP">監聽的終結點</param>        public AsyncUDPServer(IPEndPoint localEP)            : this(localEP.Address, localEP.Port,1024)        {        }        /// <summary>        /// 非同步UdpClient UDP伺服器        /// </summary>        /// <param name="localIPAddress">監聽的IP地址</param>        /// <param name="listenPort">監聽的連接埠</param>        /// <param name="maxClient">最大用戶端數量</param>        public AsyncUDPServer(IPAddress localIPAddress, int listenPort, int maxClient)        {            this.Address = localIPAddress;            this.Port = listenPort;            this.Encoding = Encoding.Default;            _maxClient = maxClient;            //_clients = new List<AsyncUDPSocketState>();            _server = new UdpClient(new IPEndPoint(this.Address, this.Port));            _recvBuffer=new byte[_server.Client.ReceiveBufferSize];        }        #endregion        #region Method        /// <summary>        /// 啟動伺服器        /// </summary>        /// <returns>非同步TCP伺服器</returns>        public void Start()        {            if (!IsRunning)            {                IsRunning = true;                _server.EnableBroadcast = true;                _server.BeginReceive(ReceiveDataAsync, null);            }        }        /// <summary>        /// 停止伺服器        /// </summary>        public void Stop()        {            if (IsRunning)            {                IsRunning = false;                _server.Close();                //TODO 關閉對所有用戶端的串連            }        }        /// <summary>        /// 接收資料的方法        /// </summary>        /// <param name="ar"></param>        private void ReceiveDataAsync(IAsyncResult ar)        {            IPEndPoint remote=null;            byte[] buffer = null;            try            {                buffer = _server.EndReceive(ar, ref remote);                //觸發資料收到事件                RaiseDataReceived(null);            }            catch (Exception)            {                //TODO 處理異常                RaiseOtherException(null);            }            finally            {                if (IsRunning && _server != null)                    _server.BeginReceive(ReceiveDataAsync, null);            }        }        /// <summary>        /// 發送資料        /// </summary>        /// <param name="msg"></param>        /// <param name="remote"></param>        public void Send(string msg, IPEndPoint remote)        {            byte[] data = Encoding.Default.GetBytes(msg);            try            {                RaisePrepareSend(null);                _server.BeginSend(data, data.Length, new AsyncCallback(SendCallback), null);            }            catch (Exception)            {                //TODO 異常處理                RaiseOtherException(null);            }        }        private void SendCallback(IAsyncResult ar)        {            if (ar.IsCompleted)            {                try                {                    _server.EndSend(ar);                    //訊息發送完畢事件                    RaiseCompletedSend(null);                }                catch (Exception)                {                    //TODO 資料發送失敗事件                    RaiseOtherException(null);                }            }        }        #endregion        #region 事件        /// <summary>        /// 接收到資料事件        /// </summary>        public event EventHandler<AsyncUDPEventArgs> DataReceived;        private void RaiseDataReceived(AsyncUDPState state)        {            if (DataReceived != null)            {                DataReceived(this, new AsyncUDPEventArgs(state));            }        }        /// <summary>        /// 發送資料前的事件        /// </summary>        public event EventHandler<AsyncUDPEventArgs> PrepareSend;        /// <summary>        /// 觸發發送資料前的事件        /// </summary>        /// <param name="state"></param>        private void RaisePrepareSend(AsyncUDPState state)        {            if (PrepareSend != null)            {                PrepareSend(this, new AsyncUDPEventArgs(state));            }        }        /// <summary>        /// 資料發送完畢事件        /// </summary>        public event EventHandler<AsyncUDPEventArgs> CompletedSend;        /// <summary>        /// 觸發資料發送完畢的事件        /// </summary>        /// <param name="state"></param>        private void RaiseCompletedSend(AsyncUDPState state)        {            if (CompletedSend != null)            {                CompletedSend(this, new AsyncUDPEventArgs(state));            }        }        /// <summary>        /// 網路錯誤事件        /// </summary>        public event EventHandler<AsyncUDPEventArgs> NetError;        /// <summary>        /// 觸發網路錯誤事件        /// </summary>        /// <param name="state"></param>        private void RaiseNetError(AsyncUDPState state)        {            if (NetError != null)            {                NetError(this, new AsyncUDPEventArgs(state));            }        }        /// <summary>        /// 例外狀況事件        /// </summary>        public event EventHandler<AsyncUDPEventArgs> OtherException;        /// <summary>        /// 觸發例外狀況事件        /// </summary>        /// <param name="state"></param>        private void RaiseOtherException(AsyncUDPState state, string descrip)        {            if (OtherException != null)            {                OtherException(this, new AsyncUDPEventArgs(descrip, state));            }        }        private void RaiseOtherException(AsyncUDPState state)        {            RaiseOtherException(state, "");        }        #endregion        #region Close        /// <summary>        /// 關閉一個與用戶端之間的會話        /// </summary>        /// <param name="state">需要關閉的用戶端工作階段對象</param>        public void Close(AsyncUDPState state)        {            if (state != null)            {                //_clients.Remove(state);                //_clientCount--;                //TODO 觸發關閉事件            }        }        /// <summary>        /// 關閉所有的用戶端工作階段,與所有的用戶端串連會斷開        /// </summary>        public void CloseAllClient()        {            //foreach (AsyncUDPSocketState client in _clients)            //{            //    Close(client);            //}            //_clientCount = 0;            //_clients.Clear();        }        #endregion        #region 釋放        /// <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 (_server != null)                        {                            _server = null;                        }                    }                    catch (SocketException)                    {                        //TODO                        RaiseOtherException(null);                    }                }                disposed = true;            }        }        #endregion    }}


用戶端資訊封裝了類

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.Net;namespace NetFrame.Net.UDP.Listener.Asynchronous{    public class AsyncUDPState    {        // Client   socket.        public UdpClient udpClient = null;        // Size of receive buffer.        public const int BufferSize = 1024;        // Receive buffer.        public byte[] buffer = new byte[BufferSize];        // Received data string.        public StringBuilder sb = new StringBuilder();        public IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);    }}

伺服器事件參數類

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace NetFrame.Net.UDP.Listener.Asynchronous{    /// <summary>    /// UdpClient 非同步UDP伺服器事件參數類    /// </summary>    public class AsyncUDPEventArgs : EventArgs    {        /// <summary>        /// 提示資訊        /// </summary>        public string _msg;        /// <summary>        /// 用戶端狀態封裝類        /// </summary>        public AsyncUDPState _state;        /// <summary>        /// 是否已經處理過了        /// </summary>        public bool IsHandled { get; set; }        public AsyncUDPEventArgs(string msg)        {            this._msg = msg;            IsHandled = false;        }        public AsyncUDPEventArgs(AsyncUDPState state)        {            this._state = state;            IsHandled = false;        }        public AsyncUDPEventArgs(string msg, AsyncUDPState state)        {            this._msg = msg;            this._state = state;            IsHandled = false;        }    }}

以上就是C#網路編程系列文章(七)之UdpClient實現非同步UDP伺服器的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.