Exemplo n.º 1
0
        public AsyncMulticastClientWithEvents(IPAddress multicastAddress, int port)
        {
            MulticastAddress = multicastAddress;
            Port             = port;
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Client = new UdpClient(port);
            }
            else
            {
                Client = new UdpClient
                {
                    ExclusiveAddressUse = false
                };
                Client.Client.Bind(new IPEndPoint(IPAddress.Any, Port));
            }

            var localIpAddresses = NetworkHelpers.GetIpAddresses();

            foreach (var localIpAddress in localIpAddresses)
            {
                try
                {
                    Client.JoinMulticastGroup(multicastAddress, localIpAddress);
                    Console.WriteLine($"{localIpAddress} Worked");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{localIpAddress} Did not work");
                }
            }

            Client.BeginReceive(ReceiveCallback, null);
        }
        public AsyncMulticastClientWithTap(IPAddress multicastAddress, int port)
        {
            MulticastAddress = multicastAddress;
            Port             = port;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Client = new UdpClient(port);
            }
            else
            {
                Client = new UdpClient
                {
                    ExclusiveAddressUse = false
                };
                Client.Client.Bind(new IPEndPoint(IPAddress.Any, Port));
            }

            CancellationSource = new CancellationTokenSource();

            Console.WriteLine(
                $"==> ExclusiveAddressUse {Client.ExclusiveAddressUse} MulticastLoopback {Client.MulticastLoopback}");

            var localIpAddresses = NetworkHelpers.GetIpAddresses();

            foreach (var localIpAddress in localIpAddresses)
            {
                try
                {
                    Client.JoinMulticastGroup(multicastAddress, localIpAddress);
                    Console.WriteLine($"{localIpAddress} Worked");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{localIpAddress} Did not work");
                }
            }

            Task.Factory.StartNew(ReceiveNextMessage, CancellationSource.Token);
        }