C# 네트워크 프로그래밍 기사 시리즈(8) UdpClient는 동기화된 UDP 서버를 구현합니다.

黄舟
풀어 주다: 2017-02-27 11:25:11
원래의
2096명이 탐색했습니다.

이 기사에서는

UdpClient 클래스를 소개하여 동기 차단 모드에서 연결 없는 UDP 패킷을 보내고 받는 간단한 방법을 제공합니다. UDP는 연결 없는 전송 프로토콜이므로 데이터를 보내고 받기 전에 원격 호스트 연결을 설정할 필요가 없습니다. 다음과 같이 기본 원격 호스트 옵션을 설정하기만 하면 됩니다.
원격 호스트 이름과 포트 번호를 매개변수로 사용하여 UdpClient 클래스의 인스턴스를 생성합니다.
UdpClient 클래스의 인스턴스를 생성하고 Connect 메서드를 호출합니다.
UdpClient에서 제공하는 모든 전송 방법을 사용하여 원격 장치로 데이터를 보낼 수 있습니다. 그런 다음 수신 메서드를 사용하여 원격 호스트로부터 데이터를 수신합니다.
팁: 기본 원격 호스트를 지정한 경우 호스트 이름이나 IPEndPoint를 사용하여 Send 메서드를 호출하지 마세요. 이렇게 하면 UdpClient에서 예외가 발생합니다.

UdpClient 메소드를 사용하면 멀티캐스트 패킷을 보내고 받을 수도 있습니다. UdpClient는 JoinMulticastGroup 메서드를 사용하여 멀티캐스트 그룹에 가입할 수 있습니다. 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;
using System.Threading;

namespace NetFrame.Net.UDP.Listener.Synchronous
{
    /// <summary>
    /// UdpClient 实现同步UDP服务器
    /// </summary>
    public class UDPServer
    {
        #region Fields
        /// <summary>
        /// 服务器程序允许的最大客户端连接数
        /// </summary>
        private int _maxClient;

        /// <summary>
        /// 当前的连接的客户端数
        /// </summary>
        //private int _clientCount;

        /// <summary>
        /// 服务器使用的异步UdpClient
        /// </summary>
        private UdpClient _server;

        /// <summary>
        /// 客户端会话列表
        /// </summary>
        //private List<UDPState> _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 UDPServer(int listenPort)
            : this(IPAddress.Any, listenPort,1024)
        {
        }

        /// <summary>
        /// 同步UdpClient UDP服务器
        /// </summary>
        /// <param name="localEP">监听的终结点</param>
        public UDPServer(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 UDPServer(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;
                new Thread(ReceiveData).Start();
            }
        }

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

            }
        }

        /// <summary>
        /// 同步接收数据的方法
        /// </summary>
        /// <param name="ar"></param>
        private void ReceiveData()
        {
            IPEndPoint remote = null;
            while (true)
            {
                byte[] buffer = null;
                try
                {
                    buffer = _server.Receive(ref remote);
                }
                catch (Exception)
                {
                    //异常处理操作
                    return;
                }
                if (buffer == null || buffer.Length == 0) return;
                //TODO 数据包收到触发事件
                RaiseDataReceived(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
            {
                _server.Send(data, data.Length, remote);
            }
            catch (Exception)
            {
                //TODO 异常处理
                RaiseOtherException(null);
            }
        }

        #endregion

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

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

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

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

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

        /// <summary>
        /// 异常事件
        /// </summary>
        public event EventHandler<UDPEventArgs> OtherException;
        /// <summary>
        /// 触发异常事件
        /// </summary>
        /// <param name="state"></param>
        private void RaiseOtherException(UDPState state, string descrip)
        {
            if (OtherException != null)
            {
                OtherException(this, new UDPEventArgs(descrip, state));
            }
        }
        private void RaiseOtherException(UDPState state)
        {
            RaiseOtherException(state, "");
        }
        #endregion

        #region Close
        /// <summary>
        /// 关闭一个与客户端之间的会话
        /// </summary>
        /// <param name="state">需要关闭的客户端会话对象</param>
        public void Close(UDPState 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.Synchronous
{
    public class UDPState
    {
        // 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.Synchronous
{
    /// <summary>
    /// UdpClient 同步UDP服务器事件类
    /// </summary>
    public class UDPEventArgs : EventArgs
    {
        /// <summary>
        /// 提示信息
        /// </summary>
        public string _msg;

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

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

        public UDPEventArgs(string msg)
        {
            this._msg = msg;
            IsHandled = false;
        }
        public UDPEventArgs(UDPState state)
        {
            this._state = state;
            IsHandled = false;
        }
        public UDPEventArgs(string msg, UDPState state)
        {
            this._msg = msg;
            this._state = state;
            IsHandled = false;
        }
    }
}
로그인 후 복사

위는 C# 네트워크 프로그래밍 시리즈 기사(8)에서 UDP 서버를 동기화하는 UdpClient의 내용입니다. PHP 중국어 웹사이트(www.php .cn)!


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!