In the previous post we’ve reviewed the multicast addressing technology and examined some of its pitfalls.
In this post we’ll dive through the implementation of a simple application that allows sending and receiving multicast data over the LAN, while enabling the selection of the network interfaces (IPEndPoint) through which the multicast traffic will be sent and received.
The application source code can be downloaded from here.
The MulticastConfiguration class encapsulates the configuration entered by the user. It exposes two factory methods that allow instantiating the class for single network hosts and for multi network hosts. The 1st factory method requires only the multicast address/port of the sender and the receiver, while the 2nd factory method also requires the unicast address of the network card of the sender and the receiver.
The TransportAgent class is in charge of creating the sockets that receive and send the data, joining to the multicast group (i.e. sending IGMP package to the router) and binding both sockets to the appropriate network interface.
All the host has to do is to read the configuration from the UI, instantiate the configuration class, and instantiate the transport class - injecting it with the configuration instance and with delegate to OnMessageReceived callback that will be called when ever data arrive.
Sending Multicast Data
Receiving Multicast Data
In order to get a feeling about the way that multicast addressing works – let’s review the IGMP message that is sent to the router when the receiver socket joins to the multicast group (see TransportAgent line 166).
Enter ‘igmp’ in Wireshark filter.
In the multicast tester application, press ‘Connect’. The ‘network card interface’ in the receiver configuration is set to my local machine upper network card address.
As a result, IGMP package was sent to the network, requesting it to add network interface with address 192.168.2.100 (which is my upper network card address) to the multicast group 18.104.22.168. Consequently, the network will deliver any multicast data sent to group 22.214.171.124 to address 192.168.2.100.
Now, In the multicast tester application, press ‘disconnect’.
As a result, IGMP package was sent to the network, requesting it to remove the network interface from the group.
Sending Multicast Data – Single Network
Enter ‘udp’ in Wireshark filter.
In the multicast tester application, leave the ‘Network Card Address’ and ‘Network Card Port’ in the sender configuration blank, press ‘Connect’, enter some string to the message text box and press ‘Send’
As a result, the string that was specified (in this case ‘aviad’) is sent form the network card 192.168.2.100, port 3704. Since we didn’t specify the network card address and port – the operation system took the liberty to select network card and port.
Sending Multicast Data – Multihomed Network
In the multicast tester application, set the sender ‘Network Card Address’ to one of your network cards address and set the ‘Network Card Port’ to 1234, press ‘Connect’, enter some string to the message text box and press ‘Send’.
As a result, the string is sent form the network card 192.168.2.100, port 1234. Since we did specify the network card address and port – the network card and the port from which the string was sent were NOT the selection of the operation system. Even though in the previous case the operation system selected the same network card that we specified, in cases where there are more than 1 enabled and active network cards in the machine - you must specify the appropriate network address or your multicast may be delivered though the wrong network.