Beispiel #1
0
        /// <summary>
        /// 侦听
        /// </summary>
        /// <param name="LocalIp">本地IP</param>
        /// <param name="Port">本地端口</param>
        public void Listen(IPAddress LocalIp, int Port)
        {
            try
            {
                IPEndPoint ipEnd = new IPEndPoint(LocalIp, Port);
                rUDP = new  RUDPSocket( );


                this.Listened   = true;//侦听
                this.ListenPort = Port;

                rUDP.Listen(Port);
                if (IsAsync)//如果采用异步通信
                {
                    rUDP.BeginReceive(new AsyncCallback(ReadCallback), null);
                }
                else//如果是同步通信
                {
                    thdUdp = new Thread(new ThreadStart(GetUDPData));
                    thdUdp.Start();
                }
            }
            catch (Exception e)
            {
                if (Sock_Error != null)
                {
                    Sock_Error(this, new SockEventArgs(e.Source + "," + e.Message));
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 异步接收数据
        /// </summary>
        /// <param name="ar"></param>
        private void ReadCallback(IAsyncResult ar)
        {
            try
            {
                IPEndPoint ipend = null;
                byte[]     RData = rUDP.EndReceive(ar);
                if (DataArrival != null)
                {
                    DataArrival(this, new SockEventArgs(RData, ipend));
                }

                rUDP.BeginReceive(new AsyncCallback(ReadCallback), null);
            }
            catch (Exception e)
            {
                if (Sock_Error != null)
                {
                    Sock_Error(this, new SockEventArgs(e.Source + "," + e.Message));
                }
            }
        }