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:


byte[] DoWork()


byte[] retVal;

UdpClient client =

new UdpClient();

Socket s = client.Client;

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


new IPEndPoint(IPAddress.Any, 9999));


""), 2);

IPEndPoint iep =

new IPEndPoint(IPAddress.Any, 0);

retVal = client.Receive(

ref iep);


return retVal;



Anthony Wong [MSFT]

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