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

來源:互聯網
上載者:User

本文介紹

在.Net中,System.Net.Sockets 命名空間為需要嚴密控制網路訪問的開發人員提供了 Windows Sockets (Winsock) 介面的託管實現。System.Net 命名空間中的所有其他網路訪問類都建立在該通訊端Socket實現之上,如TCPClient、TCPListener 和 UDPClient 類封裝有關建立到 Internet 的 TCP 和 UDP 串連的詳細資料;NetworkStream類則提供用於網路訪問的基礎資料流等,常見的許多Internet服務都可以見到Socket的蹤影,如Telnet、Http、Email、Echo等,這些服務儘管通訊協議Protocol的定義不同,但是其基礎的傳輸都是採用的Socket。 其實,Socket可以象流Stream一樣被視為一個資料通道,這個通道架設在應用程式端(用戶端)和遠程伺服器端之間,而後,資料的讀取(接收)和寫入(發送)均針對這個通道來進行。
可見,在應用程式端或者伺服器端建立了Socket對象之後,就可以使用Send/SentTo方法將資料發送到串連的Socket,或者使用Receive/ReceiveFrom方法接收來自串連Socket的資料;
針對Socket編程,.NET 架構的 Socket 類是 Winsock32 API 提供的通訊端服務的Managed 程式碼版本。其中為實現網路編程提供了大量的方法,大多數情況下,Socket 類方法只是將資料封送到它們的本機 Win32 副本中並處理任何必要的安全檢查。如果你熟悉Winsock API函數,那麼用Socket類編寫網路程式會非常容易,當然,如果你不曾接觸過,也不會太困難,跟隨下面的解說,你會發覺使用Socket類開發windows 網路應用程式原來有規可尋,它們在大多數情況下遵循大致相同的步驟。


本節介紹使用Socket來實現一個同步的UDP伺服器。

Socket同步UDP伺服器


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.Net;using System.Threading;namespace NetFrame.Net.UDP.Sock.Synchronous{    /// <summary>    /// Socket 實現同步UDP伺服器    /// </summary>    public class SocketUDPServer    {        #region Fields        /// <summary>        /// 伺服器程式允許的最大用戶端串連數        /// </summary>        private int _maxClient;        /// <summary>        /// 當前的串連的用戶端數        /// </summary>        private int _clientCount;        /// <summary>        /// 伺服器使用的同步socket        /// </summary>        private Socket _serverSock;        /// <summary>        /// 用戶端工作階段列表        /// </summary>        private List<SocketUDPState> _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>        /// 非同步Socket UDP伺服器        /// </summary>        /// <param name="listenPort">監聽的連接埠</param>        public SocketUDPServer(int listenPort)            : this(IPAddress.Any, listenPort,1024)        {        }        /// <summary>        /// 非同步Socket UDP伺服器        /// </summary>        /// <param name="localEP">監聽的終結點</param>        public SocketUDPServer(IPEndPoint localEP)            : this(localEP.Address, localEP.Port,1024)        {        }        /// <summary>        /// 非同步Socket UDP伺服器        /// </summary>        /// <param name="localIPAddress">監聽的IP地址</param>        /// <param name="listenPort">監聽的連接埠</param>        /// <param name="maxClient">最大用戶端數量</param>        public SocketUDPServer(IPAddress localIPAddress, int listenPort, int maxClient)        {            this.Address = localIPAddress;            this.Port = listenPort;            this.Encoding = Encoding.Default;            _maxClient = maxClient;            _clients = new List<SocketUDPState>();            _serverSock = new Socket(localIPAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp);            _recvBuffer=new byte[_serverSock.ReceiveBufferSize];        }        #endregion        #region Method        /// <summary>        /// 啟動伺服器        /// </summary>        /// <returns>非同步TCP伺服器</returns>        public void Start()        {            if (!IsRunning)            {                IsRunning = true;                _serverSock.Bind(new IPEndPoint(this.Address, this.Port));                //啟動一個線程監聽資料                new Thread(ReceiveData).Start();            }        }        /// <summary>        /// 停止伺服器        /// </summary>        public void Stop()        {            if (IsRunning)            {                IsRunning = false;                _serverSock.Close();                //TODO 關閉對所有用戶端的串連                CloseAllClient();            }        }        /// <summary>        /// 同步資料接收方法        /// </summary>        private void ReceiveData()        {            int len = -1;            EndPoint remote = null;            while (true)            {                try                {                    len = _serverSock.ReceiveFrom(_recvBuffer, ref remote);                    //if (!_clients.Contains(remote))                    //{                    //    _clients.Add(remote);                    //}                }                catch (Exception)                {                    //TODO 異常處理操作                    RaiseOtherException(null);                }            }        }        /// <summary>        /// 同步發送資料        /// </summary>        public void Send(string msg, EndPoint clientip)        {            byte[] data = Encoding.Default.GetBytes(msg);            try            {                _serverSock.SendTo(data, clientip);                //資料發送完成事件                RaiseCompletedSend(null);            }            catch (Exception)            {                //TODO 異常處理                RaiseOtherException(null);            }        }        #endregion        #region 事件        /// <summary>        /// 接收到資料事件        /// </summary>        public event EventHandler<SocketUDPEventArgs> DataReceived;        private void RaiseDataReceived(SocketUDPState state)        {            if (DataReceived != null)            {                DataReceived(this, new SocketUDPEventArgs(state));            }        }        /// <summary>        /// 資料發送完畢事件        /// </summary>        public event EventHandler<SocketUDPEventArgs> CompletedSend;        /// <summary>        /// 觸發資料發送完畢的事件        /// </summary>        /// <param name="state"></param>        private void RaiseCompletedSend(SocketUDPState state)        {            if (CompletedSend != null)            {                CompletedSend(this, new SocketUDPEventArgs(state));            }        }        /// <summary>        /// 網路錯誤事件        /// </summary>        public event EventHandler<SocketUDPEventArgs> NetError;        /// <summary>        /// 觸發網路錯誤事件        /// </summary>        /// <param name="state"></param>        private void RaiseNetError(SocketUDPState state)        {            if (NetError != null)            {                NetError(this, new SocketUDPEventArgs(state));            }        }        /// <summary>        /// 例外狀況事件        /// </summary>        public event EventHandler<SocketUDPEventArgs> OtherException;        /// <summary>        /// 觸發例外狀況事件        /// </summary>        /// <param name="state"></param>        private void RaiseOtherException(SocketUDPState state, string descrip)        {            if (OtherException != null)            {                OtherException(this, new SocketUDPEventArgs(descrip, state));            }        }        private void RaiseOtherException(SocketUDPState state)        {            RaiseOtherException(state, "");        }        #endregion        #region Close        /// <summary>        /// 關閉一個與用戶端之間的會話        /// </summary>        /// <param name="state">需要關閉的用戶端工作階段對象</param>        public void Close(SocketUDPState state)        {            if (state != null)            {                _clients.Remove(state);                _clientCount--;                //TODO 觸發關閉事件            }        }        /// <summary>        /// 關閉所有的用戶端工作階段,與所有的用戶端串連會斷開        /// </summary>        public void CloseAllClient()        {            foreach (SocketUDPState 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 (_serverSock != null)                        {                            _serverSock = 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.Sock.Synchronous{    public class SocketUDPState    {        // Client   socket.        public Socket workSocket = 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 EndPoint remote = new IPEndPoint(IPAddress.Any, 0);    }}


伺服器事件參數類

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace NetFrame.Net.UDP.Sock.Synchronous{    /// <summary>    /// Socket實現同步UDP伺服器    /// </summary>    public class SocketUDPEventArgs:EventArgs    {        /// <summary>        /// 提示資訊        /// </summary>        public string _msg;        /// <summary>        /// 用戶端狀態封裝類        /// </summary>        public SocketUDPState _state;        /// <summary>        /// 是否已經處理過了        /// </summary>        public bool IsHandled { get; set; }        public SocketUDPEventArgs(string msg)        {            this._msg = msg;            IsHandled = false;        }        public SocketUDPEventArgs(SocketUDPState state)        {            this._state = state;            IsHandled = false;        }        public SocketUDPEventArgs(string msg, SocketUDPState state)        {            this._msg = msg;            this._state = state;            IsHandled = false;        }    }}

以上就是C#網路編程系列文章(六)之Socket實現同步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.