0

I am working on a c# based application which is sending messages continuously using Multi-casting. Every thing works fine. The clients at thereceiving end receives messages continuously till the network is disconnected. But when I reconnect the network the client machines on the same network don't receive any messages till I collect all the messages on the same machine via receiving code.

Send Code:

      using (UdpClient udpclient = new UdpClient())
                    {
                        IPAddress multicastaddress = IPAddress.Parse("239.0.0.222");
                        try
                        {
                            udpclient.ExclusiveAddressUse = false;
                            udpclient.MulticastLoopback = false;
                            udpclient.JoinMulticastGroup(multicastaddress);
                            IPEndPoint remoteep = new IPEndPoint(multicastaddress, 8191);

                            int j = udpclient.Send(byteBuffer, byteBuffer.Length, remoteep);

                        }
                        catch (Exception e)
                        {
                            udpclient.DropMulticastGroup(multicastaddress);
                            udpclient.Close(); 
                        }
                        finally
                        {
                          udpclient.DropMulticastGroup(multicastaddress);
                          udpclient.Close();

                        }

                });

Receive Code:

            var udpClientDispose = new UdpClient(_settingsViewModel.SyncPort);

            var ipEndPoint = new IPEndPoint(IPAddress.Any, 8191);
            IPAddress multicastaddress = IPAddress.Parse("239.0.0.222");
            udpClientDispose.JoinMulticastGroup(multicastaddress, "192.168.0.12");
            var timeElapsedSinceMasterMessageReceived = new Stopwatch();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            while (sw.ElapsedMilliseconds < 5000)
            {
                udpClientDispose.Receive(ref ipEndPoint);

            }
            udpClientDispose.Close();`

It Seems like all messages are getting collected at my system and there is a network jam on the particular multicast address i.e "239.0.0.222". As if I try to change the address it works but not again on "239.0.0.222".

Anyone knows the exact reason why is this happening and any valid solution to this.

Yashwinder
  • 59
  • 8

1 Answers1

0

When you say "network is disconnected", I'm going to assume you disable the NIC or physically unplug the wire.

If you subscribe to a multicast group, the NIC driver is instructed to listen to traffic from specific MAC addresses. When the link goes down or the NIC is disabled, the NIC driver will stop to listen to that multicast group and you will have to resubscribe manually.

You can use the NetworkInformation class to subscribe to event information if a NIC goes up/down and use that to resubscribe accordingly.

TJF
  • 2,248
  • 4
  • 28
  • 43