PowerTCP Sockets for .NET
JoinMulticastGroup Method (UdpBase)
Example 




The System.Net.IPAddress specifying the multicast group address to join.
Join the specified multicast group for receiving datagrams.
Syntax
'Declaration
 
<SecuritySafeCriticalAttribute()>
Public Sub JoinMulticastGroup( _
   ByVal groupAddress As IPAddress _
) 
'Usage
 
Dim instance As UdpBase
Dim groupAddress As IPAddress
 
instance.JoinMulticastGroup(groupAddress)
[SecuritySafeCritical()]
public void JoinMulticastGroup( 
   IPAddress groupAddress
)
[SecuritySafeCritical()]
public: void JoinMulticastGroup( 
   IPAddress* groupAddress
) 
[SecuritySafeCritical()]
public:
void JoinMulticastGroup( 
   IPAddress^ groupAddress
) 

Parameters

groupAddress
The System.Net.IPAddress specifying the multicast group address to join.
Exceptions
ExceptionDescription
System.Net.Sockets.SocketExceptionThe multicast address is unknown, invalid, or unable to be resolved.
Remarks

Use the JoinMulticastGroup method to join a multicast group. Useful when you wish for datagrams to be sent to multiple hosts. The host is able to receive all datagrams sent to the specified multicast address and port.

Multicast groups are specified as an IP Address within a specified range of addresses. This group of addresses, also know as class D IP Addresses, are in the range from 224.0.0.0 through 239.255.255.255. In order to receive packets sent to a multicast group (using Receive), you must first join the group using this method.

Using multicasting over broadcasting reduces the load on hosts that are not interested in the data sent.

Example
The following example demonstrates joining a multicast group, sending data to it, receiving that data, and leaving the multicast group.
private void button1_Click(object sender, EventArgs e)
{
    udp1.Start(multicastWorker, null);
}

private void multicastWorker(object state)
{
    IPEndPoint groupEndPoint = new IPEndPoint("239.0.0.1", 32000);
    udp1.Open(32000);
    udp1.JoinMulticastGroup(groupEndPoint.Address);
    udp1.Send("hello world!", groupEndPoint);
    byte[] buffer = new byte[1024];
    Datagram datagram = udp1.Receive(buffer);
    //Marshal data to UI for display
    udp1.Marshal(datagram.ToString(), null);
    udp1.LeaveMulticastGroup(groupEndPoint.Address);
    udp1.Close();
}

void udp1_UserState(object sender, UserStateEventArgs e)
{
    textBox1.AppendText(e.Message);
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
    udp1.Start(AddressOf multicastWorker, Nothing)
End Sub

Private Sub multicastWorker(ByVal state As Object)
    Dim groupEndPoint As New IPEndPoint("239.0.0.1", 32000)
    udp1.Open(32000)
    udp1.JoinMulticastGroup(groupEndPoint.Address)
    udp1.Send("hello world!", groupEndPoint)
    Dim buffer(1023) As Byte
    Dim datagram As Datagram = udp1.Receive(buffer)
    'Marshal data to UI for display
    udp1.Marshal(datagram.ToString(), Nothing)
    udp1.LeaveMulticastGroup(groupEndPoint.Address)
    udp1.Close()
End Sub

Private Sub udp1_UserState(ByVal sender As Object, ByVal e As UserStateEventArgs)
    textBox1.AppendText(e.Message)
End Sub
See Also

Reference

UdpBase Class
UdpBase Members


PowerTCP Sockets for .NET Documentation Version 4.5
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic