UdpClient.LeaveMulticastGroup Method
Leaves the specified multicast group.
public virtual void LeaveMulticastGroup(IPAddress multicastAddress)
Parameters
- multicastAddress: An IPAddress representing the multicast group to leave.
Remarks
- This method can be used to leave a multicast group on any of the network interfaces associated with the UdpClient.
-
To leave a multicast group on a specific interface, use the overload
LeaveMulticastGroup(IPAddress, NetworkInterface). - When your application no longer needs to receive multicast datagrams on a specific group, you should call this method to release system resources and stop processing unnecessary network traffic.
- If the UdpClient has not joined the specified multicast group, this method has no effect.
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
- Namespace: System.Net.Sockets
- Assembly: System.Net.Sockets.dll