コード例 #1
0
        protected virtual void OnReceiveBroadcast(UDPBroadcastReceiveBroadcastEventArgs e)
        {
            var handler = ReceiveBroadcast;

            if (handler != null)
            {
                handler(this, e);
            }
        }
コード例 #2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            UdpClient  u  = (UdpClient)ar.AsyncState;
            IPEndPoint ep = new IPEndPoint(IPAddress.Any, m_Port);

            Byte[] receiveBytes;

            try
            {
                receiveBytes = u.EndReceive(ar, ref ep);
            }
            catch (SocketException se)
            {
                System.Diagnostics.Debug.Print("UDPBroadcast: EndReceive: {0}", se.Message);
                return;
            }

            System.Diagnostics.Debug.Print("UDPBroadcast: [{0}:{1}] {2}", ep.Address.ToString(), ep.Port.ToString(), Encoding.ASCII.GetString(receiveBytes));

            /* Generate Event */
            UDPBroadcastReceiveBroadcastEventArgs args = new UDPBroadcastReceiveBroadcastEventArgs();

            args.Address = ep.Address.ToString();
            args.Port    = ep.Port;
            args.Buffer  = receiveBytes;

            OnReceiveBroadcast(args);

            /* Read more data */
            try
            {
                u.BeginReceive(new AsyncCallback(ReceiveCallback), u);
            }
            catch (SocketException se)
            {
                System.Diagnostics.Debug.Print("UDPBroadcast: BeginReceive: {0}", se.Message);
            }
        }