您的位置:首页 > 其它

UDP通讯接收案例(组播方式)

2016-12-15 15:24 399 查看
1.通讯类:
namespace AddMyUtility

{

    public class UDPClientClass

    {
        public UDPClientClass(string groupIP, int Port, int timeOut)

        {
            _UdpClient = new UdpClient(Port);

            _UdpClient.Client.ReceiveTimeout = timeOut;

            _UdpClient.JoinMulticastGroup(IPAddress.Parse(groupIP));

            _IPEndPoint = null;

        }

        public string _groupIP;

        public int _port;

        public int _timeOut;

        public  byte[] _Buffer;

        public UdpClient _UdpClient;

        public IPEndPoint _IPEndPoint;

        public byte[] ReceivThreadEvetHandler()

        {
            try

            {

                byte[] buf = _UdpClient.Receive(ref _IPEndPoint);

                _Buffer = buf;

                return _Buffer;

            }

            catch

            {
                return new byte[0];

            }
        }
    }

  

}
2.调用:

 void ThirdUDPInit(string groupIP,int port,int timeOut)//第三组udp初始化

        {
            this._udpClientClass = new UDPClientClass(groupIP, port, timeOut);

       

        }

 public static string myAddGetIP()//获得主机IP

        {

            string strAddr = "";

            try

            {

                string strHostName = System.Net.Dns.GetHostName();

                System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

                foreach (IPAddress _ipaddress in ipEntry.AddressList)

                {

                    if (_ipaddress.AddressFamily.ToString().ToLower() == "internetwork")

                    {

                        strAddr = _ipaddress.ToString();

                        break;

                    }

                }

            }

            catch (System.Exception e)

            {
            }
            return strAddr;

        }

 void MsgRecvThread_1()//2016.12.15

        {

            try

            {

                string text;

                while (true)

                {

                    try

                    {

                        byte[] content = this._udpClientClass.ReceivThreadEvetHandler();

                        if (content.Length > 0)

                        {

                            text = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]")

                                 + "收到外部消息:"

                                 + System.Text.Encoding.Default.GetString(content);

                            CommandOutputWindow.Output(text, ScriptMessageType.Command);

                            CommandOutputWindow.Output(System.Environment.NewLine, ScriptMessageType.Command);
                        }

                    }

                    catch

                    { this._udpClientClass._UdpClient.Close();

                    this._udpClientClass._UdpClient = new UdpClient(_udpClientClass._port);

                    _udpClientClass._UdpClient.Client.ReceiveTimeout = _udpClientClass._timeOut;

                   _udpClientClass. _UdpClient.JoinMulticastGroup(IPAddress.Parse(_udpClientClass._groupIP));

                   _udpClientClass._IPEndPoint = null;

                    }

                }

            }

            catch (Exception ex)

            {

                MessageOutputWindow.Output(ex.Message, ScriptMessageType.Error);

            }

        }

 public void AddThread()//新加线程

        {
            Thread th = new Thread(new ThreadStart(MsgRecvThread_1));

            th.Start();

       

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: