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

JoinMulticastGroup() public method

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

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

            // IPv6 Changes: we need to create the correct MulticastOption and
            //               must also check for address family compatibility.
            if (multicastAddr.AddressFamily != _family)
            {
                throw new ArgumentException(SR.Format(SR.net_protocol_invalid_multicast_family, "UDP"), nameof(multicastAddr));
            }

            if (_family == AddressFamily.InterNetwork)
            {
                MulticastOption mcOpt = new MulticastOption(multicastAddr);

                _clientSocket.SetSocketOption(
                    SocketOptionLevel.IP,
                    SocketOptionName.AddMembership,
                    mcOpt);
            }
            else
            {
                IPv6MulticastOption mcOpt = new IPv6MulticastOption(multicastAddr);

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

Same methods

UdpClient::JoinMulticastGroup ( IPAddress multicastAddr, IPAddress localAddress ) : void
UdpClient::JoinMulticastGroup ( IPAddress multicastAddr, int timeToLive ) : void
UdpClient::JoinMulticastGroup ( int ifindex, IPAddress multicastAddr ) : 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