Share via


Multicast Sockets Sharing a Port

Because multicasting is a one to many communication, there maybe some occasions when you would like multiple UdpClient instances on the same machine to receive data from the same multicast group and port. To do this, you would need to set the SO_REUSEADDR socket option before joining the multicast group. Here are some sample codes that do the job:

public

byte[] DoWork()

{

byte[] retVal;

UdpClient client =

new UdpClient();

Socket s = client.Client;

s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);

s.Bind(

new IPEndPoint(IPAddress.Any, 9999));

client.JoinMulticastGroup(IPAddress.Parse(

"224.0.0.0"), 2);

IPEndPoint iep =

new IPEndPoint(IPAddress.Any, 0);

retVal = client.Receive(

ref iep);

client.Close();

return retVal;

}

Cheers,

Anthony Wong [MSFT]

This posting is provided "AS IS" with no warranties, and confers no rights.