JoinMulticastGroup Method (IPAddress, Int32)
Collapse the table of content
Expand the table of content

UdpClient.JoinMulticastGroup Method (IPAddress, Int32)


Adds a UdpClient to a multicast group with the specified Time to Live (TTL).

Namespace:   System.Net.Sockets
Assembly:  System (in System.dll)

public void JoinMulticastGroup(
	IPAddress multicastAddr,
	int timeToLive


Type: System.Net.IPAddress

The IPAddress of the multicast group to join.

Type: System.Int32

The Time to Live (TTL), measured in router hops.

Exception Condition

The TTL provided is not between 0 and 255


The underlying Socket has been closed.


An error occurred when accessing the socket. See the Remarks section for more information.


multicastAddr is null.


The IP address is not compatible with the AddressFamily value that defines the addressing scheme of the socket.

The JoinMulticastGroup method subscribes the UdpClient to a multicast group using the specified IPAddress. After calling the JoinMulticastGroup method, the underlying Socket sends an Internet Group Management Protocol (IGMP) packet to the router requesting membership to the multicast group. The multicast address range is to If you specify an address outside this range or if the router to which the request is made is not multicast enabled, UdpClient will throw a SocketException. If you receive a 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. The timeToLive parameter specifies how many router hops will be allowed for a multicasted datagram before being discarded. Once the 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 IPAddress.


You must create the UdpClient using the multicast port number otherwise you will not be able to receive multicasted datagrams. Do not call the Connect method prior to calling the JoinMulticastGroup method or the receive 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 as parameter a port or an endpoint.

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


You cannot call JoinMulticastGroup on a UdpClient constructed without a specific local port (that is, using the UdpClient() or UdpClient(AddressFamily) constructor).

The following example demonstrates how to join a multicast group by providing two parameters, a multicast address, and a number that represents the TTL.

UdpClient udpClient = new UdpClient();
// Creates an IPAddress to use to join and drop the multicast group.
IPAddress multicastIpAddress = IPAddress.Parse("");

     // The packet dies after 50 router hops.
     udpClient.JoinMulticastGroup(multicastIpAddress, 50);
catch ( Exception e ){
    Console.WriteLine( e.ToString());

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top
© 2016 Microsoft