System.Net.Sockets.UdpClient.JoinMulticastGroup C# (CSharp) Method

JoinMulticastGroup() public method

public JoinMulticastGroup ( int ifindex, IPAddress multicastAddr ) : void
ifindex int
multicastAddr IPAddress
return void
        public void JoinMulticastGroup(int ifindex, IPAddress multicastAddr)
        {
            // Validate input parameters.
            if (_cleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if (multicastAddr == null)
            {
                throw new ArgumentNullException(nameof(multicastAddr));
            }

            if (ifindex < 0)
            {
                throw new ArgumentException(SR.net_value_cannot_be_negative, nameof(ifindex));
            }

            // Ensure that this is an IPv6 client, otherwise throw WinSock 
            // Operation not supported socked exception.
            if (_family != AddressFamily.InterNetworkV6)
            {
                throw new SocketException((int)SocketError.OperationNotSupported);
            }

            IPv6MulticastOption mcOpt = new IPv6MulticastOption(multicastAddr, ifindex);

            _clientSocket.SetSocketOption(
                SocketOptionLevel.IPv6,
                SocketOptionName.AddMembership,
                mcOpt);
        }

Same methods

UdpClient::JoinMulticastGroup ( IPAddress multicastAddr ) : void
UdpClient::JoinMulticastGroup ( IPAddress multicastAddr, IPAddress localAddress ) : void
UdpClient::JoinMulticastGroup ( IPAddress multicastAddr, int timeToLive ) : void

Usage Example

        private static async Task ReaderAsync(int port, string groupAddress)
        {
            using (var client = new UdpClient(port))
            {
                if (groupAddress != null)
                {
                    client.JoinMulticastGroup(IPAddress.Parse(groupAddress));
                    WriteLine($"joining the multicast group {IPAddress.Parse(groupAddress)}");
                }

                bool completed = false;
                do
                {
                    WriteLine("starting the receiver");
                    UdpReceiveResult result = await client.ReceiveAsync();
                    byte[] datagram = result.Buffer;
                    string received = Encoding.UTF8.GetString(datagram);
                    WriteLine($"received {received}");
                    if (received == "bye")
                    {
                        completed = true;
                    }
                } while (!completed);
                WriteLine("receiver closing");

                if (groupAddress != null)
                {
                    client.DropMulticastGroup(IPAddress.Parse(groupAddress));
                }
            }
        }
All Usage Examples Of System.Net.Sockets.UdpClient::JoinMulticastGroup