.NET Documentation

UdpClient.LeaveMulticastGroup Method

Leaves the specified multicast group.
public virtual void LeaveMulticastGroup(IPAddress multicastAddress)

Parameters

Remarks

Overloads

LeaveMulticastGroup(IPAddress multicastAddress)

Leaves the specified multicast group.

LeaveMulticastGroup(IPAddress multicastAddress, NetworkInterface networkInterface)

Leaves the specified multicast group on the specified network interface.

Parameters:

  • multicastAddress: An IPAddress representing the multicast group to leave.
  • networkInterface: The NetworkInterface on which to leave the multicast group.

Example

C# Example

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

public class UdpExample
{
    public static void Main(string[] args)
    {
        // Replace with a valid multicast group address
        IPAddress multicastAddress = IPAddress.Parse("239.1.1.1");
        int port = 11000;

        using (UdpClient udpClient = new UdpClient())
        {
            try
            {
                // Join the multicast group
                udpClient.JoinMulticastGroup(multicastAddress);
                Console.WriteLine($"Joined multicast group: {multicastAddress}");

                // ... perform operations with the multicast group ...

                // Leave the multicast group
                udpClient.LeaveMulticastGroup(multicastAddress);
                Console.WriteLine($"Left multicast group: {multicastAddress}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    }
}

VB.NET Example

Imports System
Imports System.Net
Imports System.Net.Sockets

Public Module UdpExample
    Public Sub Main()
        ' Replace with a valid multicast group address
        Dim multicastAddress As IPAddress = IPAddress.Parse("239.1.1.1")
        Dim port As Integer = 11000

        Using udpClient As New UdpClient()
            Try
                ' Join the multicast group
                udpClient.JoinMulticastGroup(multicastAddress)
                Console.WriteLine($"Joined multicast group: {multicastAddress}")

                ' ... perform operations with the multicast group ...

                ' Leave the multicast group
                udpClient.LeaveMulticastGroup(multicastAddress)
                Console.WriteLine($"Left multicast group: {multicastAddress}")
            Catch ex As Exception
                Console.WriteLine($"An error occurred: {ex.Message}")
            End Try
        End Using
    End Sub
End Module

Requirements