コード例 #1
0
        public async void SendMulticastMessage(string address, int port, string message)
        {
            // typical instantiation
            var receiver = new UdpSocketMulticastClient();

            receiver.TTL = 5;

            receiver.MessageReceived += (sender, args) =>
            {
                var from = String.Format("{0}:{1}", args.RemoteAddress, args.RemotePort);
                var data = Encoding.UTF8.GetString(args.ByteData, 0, args.ByteData.Length);

                string messageReceived = string.Format("{0} - {1}", from, data);
                Console.WriteLine(messageReceived);

                InvokeMulticastMessageReceived(messageReceived);
            };

            // join the multicast address:port
            await receiver.JoinMulticastGroupAsync(address, port);

            var msgBytes = Encoding.UTF8.GetBytes(message);

            // send a message that will be received by all listening in
            // the same multicast group.
            await receiver.SendMulticastAsync(msgBytes);
        }
コード例 #2
0
        public UdpSocketMulticastClientPage()
        {
            _client      = new UdpSocketMulticastClient();
            _messagesSub = new Subject <Message>();
            _messagesObs = _messagesSub.AsObservable();

            InitView();
        }
コード例 #3
0
        public CitpUdpService(ICitpLogService log, IpAddress nicIp, bool useOriginalMulticastIp)
        {
            _log   = log;
            _nicIp = nicIp;
            _useOriginalMulticastIp = useOriginalMulticastIp;

            _client = new UdpSocketMulticastClient();
            _client.MessageReceived += messageReceived;
        }
コード例 #4
0
ファイル: Multicast.cs プロジェクト: yvanvds/interact
 private async Task Join()
 {
     if (receiver == null)
     {
         receiver     = new UdpSocketMulticastClient();
         receiver.TTL = 5;
     }
     await receiver.JoinMulticastGroupAsync(Global.MulticastAddress, Global.MulticastPort);
 }
コード例 #5
0
        public MainPage()
        {
            InitializeComponent();
            _udpSocketMulticastClient     = new UdpSocketMulticastClient();
            _udpSocketMulticastClient.TTL = 5;

            _guirlandes.Add(new Guirlande3Ligne());
            _guirlandes.Add(new GuirlandePairing());

            ChoiceGuirlande_OnValueChanged(null, new ValueChangedEventArgs(0, 0));
        }
コード例 #6
0
ファイル: Multicast.cs プロジェクト: yvanvds/interact
        public async void Disconnect()
        {
            if (joined)
            {
                await receiver.DisconnectAsync();

                joined = false;
            }
            receiver.Dispose();
            receiver = null;
        }
コード例 #7
0
        public GuirlandePage(string urlServeur, int position, int port)
        {
            _urlServeur = urlServeur;
            _position   = position;
            _port       = port;
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);

            _platformService = DependencyService.Get <IPlatformService>();
            if (Device.RuntimePlatform == Device.Android)
            {
                _platformService.HideTabBar();
            }

            _udpSocketMulticastClient     = new UdpSocketMulticastClient();
            _udpSocketMulticastClient.TTL = 5;
        }
コード例 #8
0
ファイル: Multicast.cs プロジェクト: yvanvds/interact
        public async void Start()
        {
            if (connected)
            {
                return;
            }
            connected = true;

            receiver     = new UdpSocketMulticastClient();
            receiver.TTL = 5;

            sender = new UdpSocketClient();

            receiver.MessageReceived += async(sender, args) =>
            {
                var data = Encoding.UTF8.GetString(args.ByteData, 0, args.ByteData.Length);

                if (data.Equals("INTERACT ID REQUEST"))
                {
                    var stream = new MemoryStream();
                    var writer = new BinaryWriter(stream, Encoding.UTF8);
                    writer.Write((Byte)NetworkMessage.Acknowledge);
                    writer.Write(Properties.Settings.Default.ServerName);
                    writer.Write(Properties.Settings.Default.NetworkToken);
                    writer.Write((Byte)NetworkMessage.EndOfMessage);
                    writer.Flush();

                    await this.sender.SendToAsync(stream.GetBuffer(), args.RemoteAddress, Constants.UdpPort);
                }
            };

            try
            {
                await receiver.JoinMulticastGroupAsync(Constants.MulticastAddress, Constants.MulticastPort);

                Byte[] data = Encoding.ASCII.GetBytes("ANOUNCE SERVER");
                await receiver.SendMulticastAsync(data);
            } catch (Exception e)
            {
                Log.Log.Handle.AddEntry("Error: " + e.Message);
            }
        }
コード例 #9
0
ファイル: KnxRouting.cs プロジェクト: tomyqg/ms-iot
        public async void Connect()
        {
            try
            {
                foreach (IPAddress localIp in this.IpV4Addresses)
                {
                    var client = new UdpSocketMulticastClient(); //UdpClient(new IPEndPoint(localIp, _localEndpoint.Port));
                    this.UdpClients.Add(client);
                    client.MessageReceived += this.OnMessageReceived;
                    await client.JoinMulticastGroupAsync(RemoteEndPoint.Address.ToString(), RemoteEndPoint.Port); //.JoinMulticastGroup(ConnectionConfiguration.IpAddress, localIp);
                }
            }
            catch (SocketException ex)
            {
                if (IsDebug)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }

            base.Connected();
        }
コード例 #10
0
ファイル: Program.cs プロジェクト: RostislavST/SocketLite.Rx2
        static async void Start()
        {
            var communicationInterface = new CommunicationsInterface();
            var allInterfaces          = communicationInterface.GetAllInterfaces();

            var networkInterface = allInterfaces.FirstOrDefault(x => x.IpAddress == "192.168.0.36");

            //var tcpListener = new TcpSocketListener();

            //var observerTcpListner = await tcpListener.CreateObservableListener(
            //    port:8000,
            //    communicationInterface: networkInterface,
            //    allowMultipleBindToSamePort:true);

            //var subscriberTcpListener = observerTcpListner.Subscribe(
            //    tcpClient =>
            //    {
            //        //Insert your code here
            //    },
            //    ex =>
            //    {
            //        // Insert your exception code here
            //    },
            //    () =>
            //    {
            //        // Insert your completed code here
            //    });

            //var udpReceiver = new UdpSocketReceiver();

            //var observerUdpReceiver = await udpReceiver.ObservableUnicastListener(
            //    port: 1900,
            //    communicationInterface: networkInterface,
            //    allowMultipleBindToSamePort: true);

            //var subscriberUpdReceiver = observerUdpReceiver.Subscribe(
            //    udpMsg =>
            //    {
            //        System.Console.WriteLine($"Udp package received: {udpMsg.RemoteAddress}:{udpMsg.RemotePort}");
            //    },
            //    ex =>
            //    {
            //        //Inset your exception code here
            //    },
            //    () =>
            //    {
            //        //Insert your completion code here
            //    });

            var udpMulticast = new UdpSocketMulticastClient();

            var observerUdpMulticast = await udpMulticast.ObservableMulticastListener(
                "239.255.255.250",
                1900,
                networkInterface,
                allowMultipleBindToSamePort : false);

            var subscriberUdpMilticast = observerUdpMulticast.Subscribe(
                udpMsg =>
            {
                System.Console.WriteLine($"Udp package received: {udpMsg.RemoteAddress}:{udpMsg.RemotePort}");
            },
                ex =>
            {
                //Insert your exception code here
            },
                () =>
            {
                //Insert your completion code here
            });

            //await udpMulti.JoinMulticastGroupAsync(
            //    "239.255.255.250",
            //    1900,
            //    firstUsableInterface,
            //    allowMultipleBindToSamePort: true,
            //    mcastIpv6AddressList:ipv6MultiCastAddressList);

            //var udpListener = new UdpSocketReceiver();

            //await udpListener.StartListeningAsync(1900, communicationInterface: firstUsableInterface, allowMultipleBindToSamePort: true);

            Console.WriteLine("Listening...");
        }
コード例 #11
0
        private async void StartSsdpDiscoveryListener()
        {
            var commInterface = grp.CommInterface;

            macAddr = commInterface.NativeInfo.GetPhysicalAddress().ToString();

            var udpMulticast = new UdpSocketMulticastClient();

            try
            {
                var observerUdpMulticast = await udpMulticast.ObservableMulticastListener(
                    "239.255.255.250",
                    1900,
                    commInterface.SocketLiteInfo,
                    allowMultipleBindToSamePort : false);

                var udpClient = new UdpClient();

                subscriberUdpMilticast = observerUdpMulticast.Subscribe(
                    async udpMsg =>
                {
                    var msg = Encoding.UTF8.GetString(udpMsg.ByteData);
                    if (msg.StartsWith("M-SEARCH * HTTP/1.1") && msg.Contains("ssdp:discover"))
                    {
                        Console.BackgroundColor = ConsoleColor.Green;
                        Console.ForegroundColor = ConsoleColor.Black;
                        Console.WriteLine("Responding M-SEARCH request from " + udpMsg.RemoteAddress);
                        Console.ResetColor();

                        await Task.Delay(new Random().Next(0, 3000));

                        var responses = BuildResponse();
                        foreach (var r in responses)
                        {
                            var bytes = Encoding.UTF8.GetBytes(r);
                            try
                            {
                                await udpClient.SendAsync(bytes, bytes.Length, hostname: udpMsg.RemoteAddress, port: Convert.ToInt32(udpMsg.RemotePort));
                            }
                            catch (SocketException ex)
                            {
                                Console.WriteLine($"SocketException:{ex.Message}");
                            }
                        }
                    }
                },
                    ex =>
                {
                    //Insert your exception code here
                },
                    () =>
                {
                    //Insert your completion code here
                });
            }
            catch (SocketException ex)
            {
                Console.BackgroundColor = ConsoleColor.Red;
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine($"Failed to start SSDP device profile: {ex.Message}");
                Console.ResetColor();
            }
        }