1、客户端

public class AsyncUdp
    {
        private Socket socket;
        private EndPoint localEP;
        private EndPoint remoteEP; 
        private byte[] buffer = new byte[1024];//用于存放接收消息

        public delegate void onDataReceive(byte[] data);
        public event onDataReceive OnDataReceive;

        /// <summary>
        /// 接收与发送
        /// </summary>
        /// <param name="localPort"></param>
        /// <param name="remoteIp"></param>
        /// <param name="remotePort"></param>
        public AsyncUdp(int localPort, IPAddress remoteIp, int remotePort)
        {
            try
            {
                //创建一个Socket实例
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                //关联本地终结点
                localEP =new IPEndPoint(IPAddress.Any, localPort);
                socket.Bind(localEP);
                //开始接收
                remoteEP = new IPEndPoint(remoteIp, remotePort); 
                this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, new AsyncCallback(ReceiveCallBack), null);
            }
            catch (Exception err)
            {
                throw (err);
            }
        }

        /// <summary>
        /// 发送
        /// </summary> 
        /// <param name="remoteIp"></param>
        /// <param name="remotePort"></param>
        public AsyncUdp(IPAddress remoteIp, int remotePort)
        {
            try
            {
                //创建一个Socket实例
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                remoteEP = new IPEndPoint(remoteIp, remotePort);
            }
            catch (Exception err)
            {
                throw (err);
            }
        }


        /// <summary>
        /// 接收 
        /// </summary>
        /// <param name="localPort"></param> 
        public AsyncUdp(int localPort)
        {
            try
            {
                //创建一个Socket实例
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                //关联本地终结点
                localEP = new IPEndPoint(IPAddress.Any, localPort);
                socket.Bind(localEP); 
                this.socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null); 
            }
            catch (Exception err)
            {
                throw (err);
            }
        }


        /// <summary>
        /// 接收消息的回调
        /// </summary>
        /// <param name="ar"></param>
        private void ReceiveCallBack(IAsyncResult ar)
        {
            try
            {
                int length = socket.EndReceive(ar);//结束挂起的异步读取

                byte[] data = new byte[length];
                Array.Copy(buffer, 0, data, 0, data.Length);
                OnDataReceive(data);//接收的数据

                if (remoteEP == null)
                {
                    this.socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null);
                }
                else
                {
                    this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, new AsyncCallback(ReceiveCallBack), null);
                } 
            }
            catch (Exception err)
            {
                throw (err);
            }
        }

        /// <summary>
        /// 异步发送消息
        /// </summary>
        /// <param name="data"></param>
        public void SendData(byte[] data)
        {
            try
            {
                if (socket != null && remoteEP != null)
                {
                    socket.BeginSendTo(data, 0, data.Length, SocketFlags.None, remoteEP, new AsyncCallback(SendCallBack), null);
                }
            }
            catch (Exception err)
            {
                throw (err);
            }
        }
        /// <summary>
        /// 发送数据的回调
        /// </summary>
        /// <param name="ar"></param>
        private void SendCallBack(IAsyncResult ar)
        {
            try
            {
                if (socket != null)
                {
                    int length = socket.EndSend(ar);//结束挂起的异步发送
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
        }

        /// <summary>
        /// 关闭
        /// </summary>
        public void Close()
        {
            if (socket != null)
            {
                socket.Close();
            }
        }
    }

 

2、服务端

 

/// <summary>
    /// SOCKET实现异步UDP服务器
    /// </summary>
    public class AsyncSocketUDPServer
    {
        #region Fields
        /// <summary>
        /// 服务器程序允许的最大客户端连接数
        /// </summary>
        private int _maxClient;

        /// <summary>
        /// 服务器使用的同步socket
        /// </summary>
        private Socket _serverSock;

        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 AsyncSocketUDPServer(int listenPort)
            : this(IPAddress.Any, listenPort, 1024)
        {
        }

        /// <summary>
        /// 异步Socket UDP服务器
        /// </summary>
        /// <param name="localEP">监听的终结点</param>
        public AsyncSocketUDPServer(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 AsyncSocketUDPServer(IPAddress localIPAddress, int listenPort, int maxClient)
        {
            this.Address = localIPAddress;
            this.Port = listenPort;
            this.Encoding = Encoding.Default;
            _maxClient = maxClient;
            //_clients = new List<AsyncUDPSocketState>();   
            _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));
                //_serverSock.Connect(new IPEndPoint(IPAddress.Any, 0));       
                AsyncSocketUDPState so = new AsyncSocketUDPState();
                so.workSocket = _serverSock;
                _serverSock.BeginReceiveFrom(so.buffer, 0, so.buffer.Length, SocketFlags.None,
                    ref so.remote, new AsyncCallback(ReceiveDataAsync), null);
                //EndPoint sender = new IPEndPoint(IPAddress.Any, 0);        
                //_serverSock.BeginReceiveFrom(_recvBuffer, 0, _recvBuffer.Length, SocketFlags.None,    
                //    ref sender, new AsyncCallback(ReceiveDataAsync), sender);         
                //BeginReceive 和 BeginReceiveFrom的区别是什么        
                /*_serverSock.BeginReceive(_recvBuffer, 0, _recvBuffer.Length, SocketFlags.None,        
                 * new AsyncCallback(ReceiveDataAsync), null);*/
            }
        }


        /// <summary>
        /// 停止服务器
        /// </summary>
        public void Stop()
        {
            if (IsRunning)
            {
                IsRunning = false;
                _serverSock.Close();
                //TODO 关闭对所有客户端的连接     
            }
        }

        /// <summary>
        /// 接收数据的方法
        /// </summary>
        /// <param name="ar"></param>
        private void ReceiveDataAsync(IAsyncResult ar)
        {
            AsyncSocketUDPState so = ar.AsyncState as AsyncSocketUDPState;
            //EndPoint sender = new IPEndPoint(IPAddress.Any, 0);      
            int len = -1;
            try
            {
                len = _serverSock.EndReceiveFrom(ar, ref so.remote);
                //len = _serverSock.EndReceiveFrom(ar, ref sender);       
                //EndReceiveFrom 和 EndReceive区别        
                //len = _serverSock.EndReceive(ar);       
                //TODO 处理数据      
                //触发数据收到事件       
                RaiseDataReceived(so);
            }
            catch (Exception)
            {
                //TODO 处理异常    
                RaiseOtherException(so);
            }
            finally
            {
                if (IsRunning && _serverSock != null)
                    _serverSock.BeginReceiveFrom(so.buffer, 0, so.buffer.Length, SocketFlags.None,
                        ref so.remote, new AsyncCallback(ReceiveDataAsync), so);
            }
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="remote"></param>
        public void Send(string msg, EndPoint remote)
        {
            byte[] data = Encoding.Default.GetBytes(msg);
            try
            {
                RaisePrepareSend(null);
                _serverSock.BeginSendTo(data, 0, data.Length, SocketFlags.None, remote, new AsyncCallback(SendDataEnd), _serverSock);
            }
            catch (Exception)
            {
                //TODO 异常处理         
                RaiseOtherException(null);
            }
        }

        private void SendDataEnd(IAsyncResult ar)
        {
            ((Socket)ar.AsyncState).EndSendTo(ar);
            RaiseCompletedSend(null);
        }

        #endregion

        #region 事件

        /// <summary>
        /// 接收到数据事件
        /// </summary>
        public event EventHandler<AsyncSocketUDPEventArgs> DataReceived;

        private void RaiseDataReceived(AsyncSocketUDPState state)
        {
            if (DataReceived != null)
            {
                DataReceived(this, new AsyncSocketUDPEventArgs(state));
            }
        }

        /// <summary>
        /// 发送数据前的事件
        /// </summary>
        public event EventHandler<AsyncSocketUDPEventArgs> PrepareSend;

        /// <summary>
        /// 触发发送数据前的事件
        /// </summary>
        /// <param name="state"></param>
        private void RaisePrepareSend(AsyncSocketUDPState state)
        {
            if (PrepareSend != null)
            {
                PrepareSend(this, new AsyncSocketUDPEventArgs(state));
            }
        }

        /// <summary>
        /// 数据发送完毕事件
        /// </summary>
        public event EventHandler<AsyncSocketUDPEventArgs> CompletedSend;

        /// <summary>
        /// 触发数据发送完毕的事件
        /// </summary>
        /// <param name="state"></param> 
        private void RaiseCompletedSend(AsyncSocketUDPState state)
        {
            if (CompletedSend != null)
            {
                CompletedSend(this, new AsyncSocketUDPEventArgs(state));
            }
        }

        /// <summary>
        /// 网络错误事件
        /// </summary>
        public event EventHandler<AsyncSocketUDPEventArgs> NetError;


        /// <summary>
        /// 触发网络错误事件
        /// </summary>
        /// <param name="state"></param>
        private void RaiseNetError(AsyncSocketUDPState state)
        {
            if (NetError != null)
            {
                NetError(this, new AsyncSocketUDPEventArgs(state));
            }
        }

        /// <summary>
        /// 异常事件
        /// </summary>
        public event EventHandler<AsyncSocketUDPEventArgs> OtherException;

        /// <summary>
        /// 触发异常事件
        /// </summary>
        /// <param name="state"></param>
        private void RaiseOtherException(AsyncSocketUDPState state, string descrip)   
        {      
            if (OtherException != null)       
            {          
                OtherException(this, new AsyncSocketUDPEventArgs(descrip, state)); 
            }    
        }

        private void RaiseOtherException(AsyncSocketUDPState state)
        {
            RaiseOtherException(state, "");
        }
        #endregion

        #region Close
        /// <summary>
        /// 关闭一个与客户端之间的会话
        /// </summary>
        /// <param name="state">需要关闭的客户端会话对象</param>  
        public void Close(AsyncSocketUDPState 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 (_serverSock != null)           
                        {              
                            _serverSock = null;          
                        }           
                    }                
                    catch (SocketException)   
                    {              
                        //TODO         
                        RaiseOtherException(null);          
                    }    
                }        
                disposed = true;      
            }
        }
        #endregion 
    }

    public class AsyncSocketUDPState 
    {     
        // 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);  
    }
 

    /// <summary>
    /// SOCKET 异步UDP 事件类
    /// </summary>
    public class AsyncSocketUDPEventArgs : EventArgs
    {
        /// <summary>
        /// 提示信息
        /// </summary>
        public string _msg;

        /// <summary>
        /// 客户端状态封装类
        /// </summary>
        public AsyncSocketUDPState _state;

        /// <summary>
        /// 是否已经处理过了
        /// </summary>
        public bool IsHandled { get; set; }

        public AsyncSocketUDPEventArgs(string msg)
        {
            this._msg = msg;
            IsHandled = false;
        }

        public AsyncSocketUDPEventArgs(AsyncSocketUDPState state)
        {
            this._state = state;
            IsHandled = false;
        }

        public AsyncSocketUDPEventArgs(string msg, AsyncSocketUDPState state)  
        {     
            this._msg = msg;    
            this._state = state;       
            IsHandled = false;  
        }
 
    }
服务端

 

posted on 2018-11-09 11:13  逛园子$$$  阅读(216)  评论(0编辑  收藏  举报