• C# SocketUdpServer


        public interface ISocketUdpServer
        {
            void Start();
            void Stop();
            int SendData(byte[] data, IPEndPoint remoteEndPoint);
    
            event ReceiveDataHandler ReceivedDataEvent;
            event ErrorHandler ErrorEvent;
        }
    
        public delegate void ReceiveDataHandler(SocketState state);
    
        public delegate void OnlineChangeHandler(int onlines, EndPoint client);
    
        public delegate void ErrorHandler(string error, EndPoint client);
    
        public class SocketUdpServer : ISocketUdpServer
        {
            private readonly Socket _udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            private bool _isListening;
    
            public SocketUdpServer(IPEndPoint localPoint)
            {
                _udpSocket.ReceiveBufferSize = 1024 * 8;
                _udpSocket.Bind(localPoint);
            }
    
            public Socket GetUdpSocket()
            {
                return _udpSocket;
            }
    
            public void Start()
            {
                _isListening = true;
                BeginReceive();
            }
    
            public void Stop()
            {
                _isListening = false;
            }
    
            public int SendData(byte[] data, IPEndPoint remoteEndPoint)
            {
                return _udpSocket.SendTo(data, remoteEndPoint);
            }
    
            public event ReceiveDataHandler ReceivedDataEvent;
    
            public event ErrorHandler ErrorEvent;
    
            private void BeginReceive()
            {
                if (_isListening)
                {
                    SocketState state = new SocketState { Self = _udpSocket };
                    _udpSocket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, 
                        ref state.RemotePoint, ReceiveCallback, state);
                }
            }
    
            private void ReceiveCallback(IAsyncResult ar)
            {
                var state = ar.AsyncState as SocketState;
                try
                {
                    if (state != null)
                    {
                        int receiveLen = state.Self.EndReceiveFrom(ar, ref state.RemotePoint);
                        if (receiveLen > 0)
                        {
                            byte[] receivedData = new byte[receiveLen];
                            Array.Copy(state.Buffer, 0, receivedData, 0, receiveLen);
                            state.Buffer = receivedData;
                            state.ReceivedTime = DateTime.Now;
                            ReceivedDataEvent?.Invoke(state);
                        }
                    }
                }
                catch (Exception error)
                {
                    ErrorEvent?.Invoke(error.Message, state?.RemotePoint);
                }
                finally
                {
                    if (state != null) BeginReceive();
                }
            }
    
        }
    
        public class SocketState
        {
            public byte[] Buffer = new byte[1024 * 8];
            public Socket Self;
            public EndPoint RemotePoint = new IPEndPoint(IPAddress.Any, 0);
            public DateTime ReceivedTime { get; set; }
        }
  • 相关阅读:
    hdu6314 容斥+数学
    后缀数组+指针
    F. Dominant Indices
    牛客网挑战赛19 B,C,F
    拓展欧几里得理论基础(含一定证明)
    数字
    vue的自定义树形列表组件(及数据格式转换)
    Activiti实现会签功能
    7种单例模式
    MySQL中数据类型(char(n)、varchar(n)、nchar(n)、nvarchar(n)的区别)(转)
  • 原文地址:https://www.cnblogs.com/jonney-wang/p/9953033.html
Copyright © 2020-2023  润新知