티스토리 뷰

c#

[c#/wpf/winform] UDPHelper구현(server/client)

개발자 고포고 2022. 3. 2. 12:41
반응형

[c#/wpf/winform] UDPHelper구현(server/client)

 

#정의부

#싱글톤으로 사용하면된다.

 

 public class UdpHelper
    {
        public string server_host = "127.0.0.1";
        public string server_broadcast = "255.255.255.255";
        public int receiver_port = 3333;
        public int sender_port = 4333;

        private UdpClient sender;
        private UdpClient receiver;
        private static UdpHelper _instance { get; set; }
        public static UdpHelper Instance
        {
            get
            {
                return _instance ?? (_instance = new UdpHelper());
            }
        }

        public void Setting(string server_host, int receiver_port, int sender_port)
        {
            this.server_host = server_host;
            this.receiver_port = receiver_port;
            this.sender_port = sender_port;
            Receive();
        }

        public void Setting()
        {
            Receive();
        }

        public delegate void PacketReceiveEventHandler(string code);

        public void Receive(object rECEIVER_PORT)
        {
            throw new NotImplementedException();
        }

        public event PacketReceiveEventHandler PacketReceiveEventHandlerEvent;

        UdpHelper()
        {

        }

        public void Send(string msg)
        {
            Send(msg, false);
        }

    
        public void Send(string msg, int port)
        {
            Send(msg, false, port);
        }

      

        public void Send(string msg, bool isBroadCast)
        {
            Send(msg, isBroadCast, sender_port);
        }

        public async void Send(string msg, bool isBroadCast, int port)
        {
            try
            {

                sender = new UdpClient(isBroadCast ? server_broadcast : server_host, port);
                byte[] msgToByte = DataConverter.StringToByte(msg);
                _ = await sender.SendAsync(msgToByte, msgToByte.Length);
                sender.Close();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }

        public async void SendWithIP(string msg, string ip, int port)
        {
            try
            {
                sender = new UdpClient(ip, port);
                byte[] msgToByte = DataConverter.StringToByte(msg);
                _ = await sender.SendAsync(msgToByte, msgToByte.Length);
                sender.Close();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }

        public void SendByte(byte[] msg)
        {
            SendByte(msg, sender_port);
        }

        public void SendObject(object msg)
        {
            SendObject(msg, sender_port);
        }

        public void SendObject(object msg, int port)
        {
            var bytes = ByteConverter.ObjectToByte(msg);
            SendByte(bytes, false, port);
        }

        public void SendByte(byte[] msg, int port)
        {
            SendByte(msg,false, port);
        }

        public async void SendByte(byte[] msg, bool isBroadCast, int port)
        {
            try
            {
                sender = new UdpClient(isBroadCast ? server_broadcast : server_host, port);
                byte[] msgToByte = msg;
                _ = await sender.SendAsync(msgToByte, msgToByte.Length);
                sender.Close();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }

        private void Receive()
        {
            Receive(receiver_port);
        }

        public async void Receive(int Port)
        {
            try
            {
                IPEndPoint epRemote = new IPEndPoint(IPAddress.Any, Port);
                receiver = new UdpClient(epRemote);
                var receive = await receiver.ReceiveAsync();

                //자신이 보낸 패킷은 무시
                if (!NetworkHelper.IsLocalHost(receive.RemoteEndPoint))
                {
                    //var msg = ByteConverter.ByteToObject(receive.Buffer);
                    //Debug.WriteLine(msg);
                    var msg = ByteConverter.ByteToString(receive.Buffer);
                    PacketReceiveEventHandlerEvent(msg);
                }
                receiver.Close();
                Receive();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
    }

 

 

#UdperHelper #Udp #c# #wpf #winform 

반응형
댓글
반응형