UdpClient.DropMulticastGroup Method

System.Net.Sockets

Removes the local computer's membership in the specified IP multicast group.

Syntax

public void DropMulticastGroup(IPAddress multicastAddr)

Parameters

Name Description
multicastAddr An IPAddress representing the multicast group from which to remove membership.

Remarks

This method allows a client to leave a multicast group it has previously joined using the JoinMulticastGroup method. When you drop membership from a multicast group, your UdpClient instance will no longer receive datagrams sent to that group.

If the local computer has joined a multicast group multiple times, this method will only decrement the reference count. Membership will only be fully dropped when the reference count reaches zero.

The DropMulticastGroup method is the counterpart to the JoinMulticastGroup method.

Exceptions

ArgumentNullException: The multicastAddr parameter is null.

SocketException: A socket error occurred. For example, the specified IP address is not a valid multicast address.

Return Value

void

Example


using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class UdpExample
{
    public static void Main(string[] args)
    {
        try
        {
            // Define the multicast group address
            IPAddress multicastGroup = IPAddress.Parse("239.255.0.1");
            int port = 13000;

            // Create a UdpClient for sending and receiving
            using (UdpClient sender = new UdpClient())
            using (UdpClient receiver = new UdpClient(port))
            {
                // Join the multicast group
                receiver.JoinMulticastGroup(multicastGroup);
                Console.WriteLine($"Joined multicast group: {multicastGroup}");

                // Send a message to the multicast group
                byte[] sendBytes = Encoding.ASCII.GetBytes("Hello Multicast!");
                sender.Send(sendBytes, sendBytes.Length, new IPEndPoint(multicastGroup, port));
                Console.WriteLine($"Sent message to {multicastGroup}:{port}");

                // Receive a message from the multicast group
                IPEndPoint remoteEP = null;
                byte[] receiveBytes = receiver.Receive(ref remoteEP);
                string receivedData = Encoding.ASCII.GetString(receiveBytes);
                Console.WriteLine($"Received: {receivedData} from {remoteEP}");

                // Drop membership from the multicast group
                receiver.DropMulticastGroup(multicastGroup);
                Console.WriteLine($"Dropped membership from multicast group: {multicastGroup}");

                // Attempt to send again - this message will not be received by this client
                Console.WriteLine("Attempting to send again after dropping membership...");
                sender.Send(sendBytes, sendBytes.Length, new IPEndPoint(multicastGroup, port));
                Console.WriteLine("Sent another message.");

                // Wait a moment to ensure the second message is processed if it were to be received
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine("Example finished. No further messages should be received.");
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine($"SocketException: {e.Message}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"General Exception: {e.Message}");
        }
    }
}