0

I am trying to implement some Source-Specific Multicasting in c# (mainly to be free to pick any group address over the internet without risking any conflict). I followed that method, but I keep running into an error :

an unknown invalid or unsupported option or level was specified in a getsockopt or setsockopt call

This is a classical error that happens for example when you specify a SocketOptionLevel which is inconsistent with the SocketOptionName. Yet I checked that I use SocketOptionLevel.IP, which is the right one for multicasting options. I also tried the other ones, which raise the same error. Have you got any idea about what else could raise this exception, or what am I doing wrong ?

Here is my code :

mySocket = UdpClient(localEndPoint);

Buffer.BlockCopy(multicastGroupAddress.GetAddressBytes(), 0, membershipAddresses, 0, 4);
Buffer.BlockCopy(remoteEndPoint.Address.GetAddressBytes(), 0, membershipAddresses, 4, 4);
Buffer.BlockCopy(localEndPoint.Address.GetAddressBytes(), 0, membershipAddresses, 8, 4);

mySocket.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddSourceMembership, membershipAddresses);

(Yes, obviously copied from the link, I am still trying to make it work)

Thanks !

DrZed
  • 1
  • 1
  • See msdn : https://msdn.microsoft.com/en-us/library/ekd1t784(v=vs.110).aspx – jdweng Jul 19 '18 at 12:51
  • Hi jdweng, thanks for you answer but I've checked msdn already, it is well documented for "classic" multicast, but I can't find any example of source-specific multicast. – DrZed Jul 19 '18 at 13:11

1 Answers1

0

I got following code to work :

Socket mySocket = new Socket(AddressFamily.InterNetwork,
                     SocketType.Dgram,
                     ProtocolType.Udp);

// Join or create a multicast group
IPAddress multicastGroupAddress = IPAddress.Parse("224.1.1.1");

EndPoint localEndPoint  = (EndPoint)new IPEndPoint(IPAddress.Any, 2000);
mySocket.Bind(localEndPoint);
MulticastOption mcastOption = new MulticastOption(multicastGroupAddress);

mySocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption);
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • This code works perfectly for multicasting in the usual way. What I am trying to implement is **Source-Specific** multicasting. It is a type of multicasting where you join the group specifying from which specific source who is sending messages to the group you wanna receive datagrams. It shall be using _SocketOptionName.AddSourceMembership_. [This quick Wikipedia article](https://en.wikipedia.org/wiki/Source-specific_multicast) defines it, [This msdn page](https://msdn.microsoft.com/en-us/library/system.net.sockets.socketoptionname(v=vs.110).aspx) seems to point out that the option exists. – DrZed Jul 19 '18 at 15:39
  • I've checked through the Microsoft Source code and do not see it implemented. It looks like Microsoft included an enumeration for the option and then didn't implement any code to use the option. – jdweng Jul 19 '18 at 16:29
  • Okay, thank you very much for checking. I was hoping it worked because of [the other](https://stackoverflow.com/questions/5145804/how-to-join-source-specific-multicast-group-in-c-sharp-igmpv3) question, which seems to work for the guy, even though I found very little references to it too. – DrZed Jul 19 '18 at 17:02
  • I have not seen any postings indicating AddSourceMembership really works. – jdweng Jul 19 '18 at 17:25
  • On [this link https://stackoverflow.com/questions/5145804/how-to-join-source-specific-multicast-group-in-c-sharp-igmpv3](https://stackoverflow.com/questions/5145804/how-to-join-source-specific-multicast-group-in-c-sharp-igmpv3), the comment on the answer, from the poster of the question, says that it works – DrZed Jul 19 '18 at 17:33
  • That was back in 2011 and the source code could of changed. – jdweng Jul 19 '18 at 17:42
  • Okay then, I will take that in account. As _AddSourceMembership_ was still part of the enumeration, I assumed it still worked. Thanks for your time and answers ! – DrZed Jul 19 '18 at 17:44