System.Net.Sockets.UdpClient.JoinMulticastGroup Method

Adds a System.Net.Sockets.UdpClient to a multicast group.

Syntax

public void JoinMulticastGroup (System.Net.IPAddress multicastAddr)

Parameters

multicastAddr
The multicast System.Net.IPAddress of the group you want to join.

Remarks

The UdpClient.JoinMulticastGroup(System.Net.IPAddress) method subscribes the System.Net.Sockets.UdpClient to a multicast group using the specified System.Net.IPAddress. After calling the UdpClient.JoinMulticastGroup(System.Net.IPAddress) method, the underlying System.Net.Sockets.Socket sends an Internet Group Management Protocol (IGMP) packet to the router requesting membership to the multicast group. The multicast address range is 224.0.0.0 to 239.255.255.255. If you specify an address outside this range or if the router to which the request is made is not multicast enabled, System.Net.Sockets.UdpClient will throw a System.Net.Sockets.SocketException. If you receive a System.Net.Sockets.SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation in MSDN for a detailed description of the error. Once the System.Net.Sockets.UdpClient is listed with the router as a member of the multicast group, it will be able to receive multicasted datagrams sent to the specified System.Net.IPAddress.

Note:

You must create the System.Net.Sockets.UdpClient using the multicast port number; otherwise, you will not be able to receive multicasted datagrams. Do not call the UdpClient.Connect(string, int) method prior to calling the UdpClient.JoinMulticastGroup(System.Net.IPAddress) method, or the UdpClient.Receive(System.Net.IPEndPoint@) method will not work. You do not need to belong to a multicast group to send datagrams to a multicast IP address.

Before joining a multicast group, make sure the socket is bound to the port or endpoint. You do that by calling one of the constructors that accept a port or an endpoint as a parameter.

To stop receiving multicasted datagrams, call the UdpClient.DropMulticastGroup(System.Net.IPAddress) method and provide the System.Net.IPAddress of the group from which you would like to withdraw.

Note:

In the IPv6 case, there are several multicast address ranges you can choose from. Please, refer to the IETF RFC 2375.

Note:

You cannot call erload:System.Net.Sockets.UdpClient.JoinMulticastGroup on a System.Net.Sockets.UdpClient constructed without a specific local port (that is, using the UdpClient.#ctor or UdpClient.#ctor(AddressFamily) constructor).

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0