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

來源:互聯網
上載者:User

開篇

本人因為對於網路編程的喜愛,經常性的使用c#編寫各類伺服器(e.g TCP伺服器,UDP伺服器),但是基本上都是搞著玩,網上也有很多講c#網路編程的文章,當然我也參考了很多作者寫的文章。看了這篇文章以後再也不用匯出找資料了。

本系列文章會依次介紹使用Socket實現的非同步TCP伺服器、同步TCP伺服器、非同步UDP伺服器、同步UDP伺服器 and 使用TcpListener和UdpClient實現的非同步TCP伺服器、同步TCP伺服器、非同步UDP伺服器、同步UDP伺服器。


Socket非同步TCP伺服器

相信搞過網路編程的人來說這個TCP一點也不陌生吧,在C#中微軟已經幫我們封裝過了一個TcpListener和TcpClient這兩個類了,實現了對於通訊端的封裝,但是呢實際上還是不怎麼好用,所以我們用Socket來實現一個非同步TCP伺服器。

在本文中我只給出伺服器端代碼,用戶端代碼自己可以找找別處,畢竟我只是為了寫出一個好的伺服器端

下面是代碼


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;using System.Net;namespace NetFrame.Net.TCP.Sock.Asynchronous{    /// <summary>    /// Socket實現的非同步TCP伺服器    /// </summary>    public class AsyncSocketTCPServer : IDisposable    {        #region Fields        /// <summary>        /// 伺服器程式允許的最大用戶端串連數        /// </summary>        private int _maxClient;        /// <summary>        /// 當前的串連的用戶端數        /// </summary>        private int _clientCount;        /// <summary>        /// 伺服器使用的非同步socket        /// </summary>        private Socket _serverSock;        /// <summary>        /// 用戶端工作階段列表        /// </summary>        private List<AsyncSocketState> _clients;        private bool disposed = false;        #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 TCP伺服器        /// </summary>        /// <param name="listenPort">監聽的連接埠</param>        public AsyncSocketTCPServer(int listenPort)            : this(IPAddress.Any, listenPort,1024)        {        }        /// <summary>        /// 非同步Socket TCP伺服器        /// </summary>        /// <param name="localEP">監聽的終結點</param>        public AsyncSocketTCPServer(IPEndPoint localEP)            : this(localEP.Address, localEP.Port,1024)        {        }        /// <summary>        /// 非同步Socket TCP伺服器        /// </summary>        /// <param name="localIPAddress">監聽的IP地址</param>        /// <param name="listenPort">監聽的連接埠</param>        /// <param name="maxClient">最大用戶端數量</param>        public AsyncSocketTCPServer(IPAddress localIPAddress, int listenPort,int maxClient)        {            this.Address = localIPAddress;            this.Port = listenPort;            this.Encoding = Encoding.Default;            _maxClient = maxClient;            _clients = new List<AsyncSocketState>();            _serverSock = new Socket(localIPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);        }        #endregion        #region Method        /// <summary>        /// 啟動伺服器        /// </summary>        public void Start()        {            if (!IsRunning)            {                IsRunning = true;                _serverSock.Bind(new IPEndPoint(this.Address, this.Port));                _serverSock.Listen(1024);                _serverSock.BeginAccept(new AsyncCallback(HandleAcceptConnected), _serverSock);            }        }        /// <summary>        /// 啟動伺服器        /// </summary>        /// <param name="backlog">        /// 伺服器所允許的掛起串連序列的最大長度        /// </param>        public void Start(int backlog)        {            if (!IsRunning)            {                IsRunning = true;                _serverSock.Bind(new IPEndPoint(this.Address, this.Port));                _serverSock.Listen(backlog);                _serverSock.BeginAccept(new AsyncCallback(HandleAcceptConnected), _serverSock);            }        }        /// <summary>        /// 停止伺服器        /// </summary>        public void Stop()        {            if (IsRunning)            {                IsRunning = false;                _serverSock.Close();                //TODO 關閉對所有用戶端的串連            }        }        /// <summary>        /// 處理用戶端串連        /// </summary>        /// <param name="ar"></param>        private void HandleAcceptConnected(IAsyncResult ar)        {            if (IsRunning)            {                Socket server = (Socket)ar.AsyncState;                Socket client = server.EndAccept(ar);                //檢查是否達到最大的允許的用戶端數目                if (_clientCount >= _maxClient)                {                    //C-TODO 觸發事件                    RaiseOtherException(null);                }                else                {                    AsyncSocketState state = new AsyncSocketState(client);                    lock (_clients)                    {                        _clients.Add(state);                        _clientCount++;                        RaiseClientConnected(state); //觸發用戶端串連事件                    }                    state.RecvDataBuffer = new byte[client.ReceiveBufferSize];                    //開始接受來自該用戶端的資料                    client.BeginReceive(state.RecvDataBuffer, 0, state.RecvDataBuffer.Length, SocketFlags.None,                     new AsyncCallback(HandleDataReceived), state);                }                //接受下一個請求                server.BeginAccept(new AsyncCallback(HandleAcceptConnected), ar.AsyncState);            }        }        /// <summary>        /// 處理用戶端資料        /// </summary>        /// <param name="ar"></param>        private void HandleDataReceived(IAsyncResult ar)        {            if (IsRunning)            {                AsyncSocketState state = (AsyncSocketState)ar.AsyncState;                Socket client = state.ClientSocket;                try                {                    //如果兩次開始了非同步接收,所以當用戶端退出的時候                    //會兩次執行EndReceive                    int recv = client.EndReceive(ar);                    if (recv == 0)                    {                        //C- TODO 觸發事件 (關閉用戶端)                        Close(state);                        RaiseNetError(state);                        return;                    }                    //TODO 處理已經讀取的資料 ps:資料在state的RecvDataBuffer中                    //C- TODO 觸發資料接收事件                    RaiseDataReceived(state);                }                catch (SocketException)                {                    //C- TODO 異常處理                    RaiseNetError(state);                }                finally                {                    //繼續接收來自來用戶端的資料                    client.BeginReceive(state.RecvDataBuffer, 0, state.RecvDataBuffer.Length, SocketFlags.None,                     new AsyncCallback(HandleDataReceived), state);                }            }        }        /// <summary>        /// 發送資料        /// </summary>        /// <param name="state">接收資料的用戶端工作階段</param>        /// <param name="data">資料報文</param>        public void Send(AsyncSocketState state, byte[] data)        {            RaisePrepareSend(state);            Send(state.ClientSocket, data);        }        /// <summary>        /// 非同步發送資料至指定的用戶端        /// </summary>        /// <param name="client">用戶端</param>        /// <param name="data">報文</param>        public void Send(Socket client, byte[] data)        {            if (!IsRunning)                throw new InvalidProgramException("This TCP Scoket server has not been started.");            if (client == null)                throw new ArgumentNullException("client");            if (data == null)                throw new ArgumentNullException("data");            client.BeginSend(data, 0, data.Length, SocketFlags.None,             new AsyncCallback(SendDataEnd), client);        }        /// <summary>        /// 發送資料完成處理函數        /// </summary>        /// <param name="ar">勘探端Socket</param>        private void SendDataEnd(IAsyncResult ar)        {            ((Socket)ar.AsyncState).EndSend(ar);            RaiseCompletedSend(null);        }        #endregion        #region 事件        /// <summary>        /// 與用戶端的串連已建立事件        /// </summary>        public event EventHandler<AsyncSocketEventArgs> ClientConnected;        /// <summary>        /// 與用戶端的串連已斷開事件        /// </summary>        public event EventHandler<AsyncSocketEventArgs> ClientDisconnected;        /// <summary>        /// 觸發用戶端串連事件        /// </summary>        /// <param name="state"></param>        private void RaiseClientConnected(AsyncSocketState state)        {            if (ClientConnected != null)            {                ClientConnected(this, new AsyncSocketEventArgs(state));            }        }        /// <summary>        /// 觸發用戶端串連斷開事件        /// </summary>        /// <param name="client"></param>        private void RaiseClientDisconnected(Socket client)        {            if (ClientDisconnected != null)            {                ClientDisconnected(this, new AsyncSocketEventArgs("串連斷開"));            }        }        /// <summary>        /// 接收到資料事件        /// </summary>        public event EventHandler<AsyncSocketEventArgs> DataReceived;        private void RaiseDataReceived(AsyncSocketState state)        {            if (DataReceived != null)            {                DataReceived(this, new AsyncSocketEventArgs(state));            }        }        /// <summary>        /// 發送資料前的事件        /// </summary>        public event EventHandler<AsyncSocketEventArgs> PrepareSend;        /// <summary>        /// 觸發發送資料前的事件        /// </summary>        /// <param name="state"></param>        private void RaisePrepareSend(AsyncSocketState state)        {            if (PrepareSend != null)            {                PrepareSend(this, new AsyncSocketEventArgs(state));            }        }        /// <summary>        /// 資料發送完畢事件        /// </summary>        public event EventHandler<AsyncSocketEventArgs> CompletedSend;                /// <summary>        /// 觸發資料發送完畢的事件        /// </summary>        /// <param name="state"></param>        private void RaiseCompletedSend(AsyncSocketState state)        {            if (CompletedSend != null)            {                CompletedSend(this, new AsyncSocketEventArgs(state));            }        }        /// <summary>        /// 網路錯誤事件        /// </summary>        public event EventHandler<AsyncSocketEventArgs> NetError;        /// <summary>        /// 觸發網路錯誤事件        /// </summary>        /// <param name="state"></param>        private void RaiseNetError(AsyncSocketState state)        {            if (NetError != null)            {                NetError(this, new AsyncSocketEventArgs(state));            }        }        /// <summary>        /// 例外狀況事件        /// </summary>        public event EventHandler<AsyncSocketEventArgs> OtherException;        /// <summary>        /// 觸發例外狀況事件        /// </summary>        /// <param name="state"></param>        private void RaiseOtherException(AsyncSocketState state, string descrip)        {            if (OtherException != null)            {                OtherException(this, new AsyncSocketEventArgs(descrip, state));            }        }        private void RaiseOtherException(AsyncSocketState state)        {            RaiseOtherException(state, "");        }        #endregion        #region Close        /// <summary>        /// 關閉一個與用戶端之間的會話        /// </summary>        /// <param name="state">需要關閉的用戶端工作階段對象</param>        public void Close(AsyncSocketState state)        {            if (state != null)            {                state.Datagram = null;                state.RecvDataBuffer = null;                _clients.Remove(state);                _clientCount--;                //TODO 觸發關閉事件                state.Close();            }        }        /// <summary>        /// 關閉所有的用戶端工作階段,與所有的用戶端串連會斷開        /// </summary>        public void CloseAllClient()        {            foreach (AsyncSocketState 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;namespace NetFrame.Net.TCP.Sock.Asynchronous{    /// <summary>    /// 非同步Socket TCP事件參數類    /// </summary>    public class AsyncSocketEventArgs:EventArgs    {        /// <summary>        /// 提示資訊        /// </summary>        public string _msg;        /// <summary>        /// 用戶端狀態封裝類        /// </summary>        public AsyncSocketState _state;        /// <summary>        /// 是否已經處理過了        /// </summary>        public bool IsHandled { get; set; }        public AsyncSocketEventArgs(string msg)        {            this._msg = msg;            IsHandled = false;        }        public AsyncSocketEventArgs(AsyncSocketState state)        {            this._state = state;            IsHandled = false;        }        public AsyncSocketEventArgs(string msg, AsyncSocketState state)        {            this._msg = msg;            this._state = state;            IsHandled = false;        }    }}


使用者狀態封裝

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Sockets;namespace NetFrame.Net.TCP.Sock.Asynchronous{    /// <summary>    /// 非同步SOCKET TCP 中用來儲存用戶端狀態資訊的類    /// </summary>    public class AsyncSocketState    {        #region 欄位        /// <summary>        /// 接收資料緩衝區        /// </summary>        private byte[] _recvBuffer;        /// <summary>        /// 用戶端發送到伺服器的報文        /// 注意:在有些情況下報文可能只是報文的片斷而不完整        /// </summary>        private string _datagram;        /// <summary>        /// 用戶端的Socket        /// </summary>        private Socket _clientSock;        #endregion        #region 屬性        /// <summary>        /// 接收資料緩衝區         /// </summary>        public byte[] RecvDataBuffer        {            get            {                return _recvBuffer;            }            set            {                _recvBuffer = value;            }        }        /// <summary>        /// 存取會話的報文        /// </summary>        public string Datagram        {            get            {                return _datagram;            }            set            {                _datagram = value;            }        }        /// <summary>        /// 獲得與用戶端工作階段關聯的Socket對象        /// </summary>        public Socket ClientSocket        {            get            {                return _clientSock;            }        }        #endregion        /// <summary>        /// 建構函式        /// </summary>        /// <param name="cliSock">會話使用的Socket串連</param>        public AsyncSocketState(Socket cliSock)        {            _clientSock = cliSock;        }        /// <summary>        /// 初始化資料緩衝區        /// </summary>        public void InitBuffer()        {            if (_recvBuffer == null&&_clientSock!=null)            {                _recvBuffer=new byte[_clientSock.ReceiveBufferSize];            }        }        /// <summary>        /// 關閉會話        /// </summary>        public void Close()        {            //關閉資料的接受和發送            _clientSock.Shutdown(SocketShutdown.Both);            //清理資源            _clientSock.Close();        }    }}

以上就是C#網路編程系列文章(一)之Socket實現非同步TCP伺服器的內容,更多相關內容請關注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.