1

I'm new to this networking stuff to configure and I'm trying to understand the following code.

    var Server = new UdpClient();
    var multicastIp = IPAddress.Parse(_connectionParams[0]);
    IPAddress localIp;
    if (IPAddress.TryParse(_connectionParams[1], out localIp))
      Server.JoinMulticastGroup(multicastIp, localIp);
    else
      Server.JoinMulticastGroup(multicastIp);
    var endPoint = new IPEndPoint(multicastIp, int.Parse(_connectionParams[2]));

Based on my understanding, Multicasting is sending the data to the multicast ip (like 233.7.6.5) through router and the receiver might need to join the group to receive data.

    Server.JoinMulticastGroup(multicastIp, localIp); 

On the above line, What is the use of localIp here? providing localip will unicast the data to particular ip? or something else that I need to understand.

No clues in Microsoft documentation: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.udpclient.joinmulticastgroup?view=netframework-4.8#System_Net_Sockets_UdpClient_JoinMulticastGroup_System_Net_IPAddress_System_Net_IPAddress_

Gopichandar
  • 2,742
  • 2
  • 24
  • 54
  • A connection need two points a source and destination. So you need a local virtual endpoint for the source. – jdweng Sep 18 '19 at 11:23
  • @jdweng Does the local virtual endpoint matters here? Anyway data flow through multicast. Can you guide me on this pls. – Gopichandar Sep 18 '19 at 16:53
  • Yes you need the endpoints. Multicast is usually within one subnet and routers do not forward multicast (unless allow multicast property is set on router). See msdn : See msdn : https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.udpclient.joinmulticastgroup?view=netframework-4.8 – jdweng Sep 18 '19 at 17:16

1 Answers1

2
Server.JoinMulticastGroup(multicastIp, localIp); 

In IPv4, a localIp can be provided as the way to look up a physical interface, if you don't choose a local interface or select the wildcard INADDR_ANY(0.0.0.0), the system gets to decide which of your interfaces it will join the group on, which might not be what you want on a system with many physical interfaces.

An OS is only allowed to create its own rules in multicast specifications for very narrow things, i.e. selecting the default interface in this case, so you can rely on any systems documentation you find helpful to determine what steps take what input, for example: linux's tldp documentation.

lossleader
  • 13,182
  • 2
  • 31
  • 45
  • . Physical interface implies the actual ip assigned to the machine on LAN network. It helps to pick the right adaptor if there is multiple netwoek adaptors. Right? something that I will get from ipconfig cmd. if vpn connected, multiple nw adaptors will be there and giving localip will help to identify the right nw adaptors. correct me if I'm wrong. – Gopichandar Sep 18 '19 at 17:57
  • yes, some OSes also have what amount to interface aliases to hang additional ips off of.. you would ideally want the main ip that "owns" the ethernet address or owns a tunnel in a vpn and that should decide to send out igmp packets on etc.. The os is supposed to manage when to externally indicate dropped membership, etc so calls aren't purely 1-to-1 with what you management packets you see on the network as you can subscribe many sockets on one host, etc. – lossleader Sep 18 '19 at 18:23
  • @Gopichandar this example uses the pedantic way to set it with an example and a reference to finding it with ipconfig: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.multicastoption?view=netframework-4.8 and then the code alternative to ipconfig https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterface?view=netframework-4.8 and the possibility to use nw indexes instead of ips which is clearer and how it is done (via scopes) in v6. – lossleader Sep 18 '19 at 21:36