Example #1
0
        internal static void Join()
        {
            // Initialize the receive buffer
            _receiveBuffer = new byte[MAX_MESSAGE_SIZE];

            // Create the UdpAnySourceMulticastClient instance using the defined
            // GROUP_ADDRESS and GROUP_PORT constants. UdpAnySourceMulticastClient is a
            // client receiver for multicast traffic from any source, also known as Any Source Multicast (ASM)
            _client = new UdpAnySourceMulticastClient(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT);

            // Make a request to join the group.
            _client.BeginJoinGroup(
                result =>
            {
                // Complete the join
                _client.EndJoinGroup(result);

                // The MulticastLoopback property controls whether you receive multicast
                // packets that you send to the multicast group. Default value is true,
                // meaning that you also receive the packets you send to the multicast group.
                // To stop receiving these packets, you can set the property following to false
                _client.MulticastLoopback = true;

                // Set a flag indicating that we have now joined the multicast group
                _joined = true;

                Receive();
            }, null);
        }
Example #2
0
        private static UdpAnySourceMulticastClient CreateMulticastClientAndJoinGroup(string ipAddress, int localPort)
        {
            var retVal = new UdpAnySourceMulticastClient(IPAddress.Parse(ipAddress), localPort);

            var signal = new System.Threading.ManualResetEvent(false);

            try
            {
                retVal.BeginJoinGroup(
                    (joinResult) =>
                {
                    retVal.EndJoinGroup(joinResult);
                    retVal.MulticastLoopback = true;
                    retVal.SendBufferSize    = retVal.ReceiveBufferSize = SsdpConstants.DefaultUdpSocketBufferSize;

                    signal.Set();
                }, null);

                signal.WaitOne();
            }
            finally
            {
                signal.Dispose();
            }
            return(retVal);
        }
Example #3
0
        public void StartFinder()
        {
            try
            {
                _receiveBuffer = new byte[MAX_MESSAGE_SIZE];

                // Create the UdpAnySourceMulticastClient instance using the defined
                // GROUP_ADDRESS and GROUP_PORT constants. UdpAnySourceMulticastClient is a
                // client receiver for multicast traffic from any source, also known as Any Source Multicast (ASM)
                _client = new UdpAnySourceMulticastClient(IPAddress.Parse(GROUP_ADDRESS), GROUP_PORT);

                // Make a request to join the group.
                _client.BeginJoinGroup(
                    result =>
                {
                    // Complete the join
                    _client.EndJoinGroup(result);

                    // The MulticastLoopback property controls whether you receive multicast
                    // packets that you send to the multicast group. Default value is true,
                    // meaning that you also receive the packets you send to the multicast group.
                    // To stop receiving these packets, you can set the property following to false
                    _client.MulticastLoopback = false;

                    // Set a flag indicating that we have now joined the multicast group
                    _joined = true;

                    // Wait for data from the group. This is an asynchronous operation
                    // and will not block the UI thread.
                    Receive();
                }, null);
            }
            catch { }
        }
        // コンストラクター
        public MainPage()
        {
            InitializeComponent();

            // 100ミリ秒毎に満了する様にタイマーを開始
            readTimer          = new System.Windows.Threading.DispatcherTimer();
            readTimer.Tick    += new EventHandler(readTimer_Tick);
            readTimer.Interval = TimeSpan.FromMilliseconds(100);
            //readTimer.Start();

            try {
                canvas1.Width  = width;
                canvas1.Height = height;

                myClient.BeginJoinGroup(
                    result =>
                {
                    try {
                        myClient.EndJoinGroup(result);
                        myClient.MulticastLoopback = true;
                    }
                    catch (Exception ex) {
                        MessageBox.Show("Join succeeded. but something wrong. " + ex.Message);
                    }
                }, null
                    );

                Receive();
            }
            catch (Exception ex) {
                MessageBox.Show("Join failed. " + ex.Message);
            }
        }
Example #5
0
        public void Find(Action <IPAddress> callback)
        {
            FoundCallback = callback;

            MulticastSocket = new UdpAnySourceMulticastClient(IPAddress.Parse("239.255.255.250"), PortNumber);
            MulticastSocket.BeginJoinGroup((result) =>
            {
                try
                {
                    MulticastSocket.EndJoinGroup(result);
                    GroupJoined(result);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("EndjoinGroup exception {0}", ex.Message);
                    // This can happen eg when wifi is off
                    FoundCallback(null);
                }
            },
                                           null);
        }
Example #6
0
        public IPAddress Find()
        {
            WaitEvent.Reset();
            MulticastSocket = new UdpAnySourceMulticastClient(IPAddress.Parse("239.255.255.250"), PortNumber);
            MulticastSocket.BeginJoinGroup((result) =>
            {
                try
                {
                    MulticastSocket.EndJoinGroup(result);
                    GroupJoined(result);
                }
                catch (Exception ex)
                {
                    WaitEvent.Set();
                    Debug.WriteLine("EndjoinGroup exception {0}", ex.Message);
                }
            },
                                           null);

            WaitEvent.WaitOne();

            return(FoundIPAddress);
        }