UDP multicast stream suddenly stops for 1-30 secs and automatically starts again
01:52 12 Aug 2025

I am trying to receive an UDP multicast data stream with an UdpClient (C# on .NET 8). The messages are binary encoded messages, size approx. 90 bytes each, approx. 50-60 messages per second.

Receiving the data stream starts quickly, but suddenly stops after few minutes. Then no messages are received for 1...30 secs. Then the reception of messages automatically continues.

The receiver works like this:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

public class TestUDPReceiver
{
    static void Main(string[] args)
    {
        UDPReceiver test = new UDPReceiver();
        test.Init();
        test.Start();

        bool end = false;

        while (!end)
        {
            if (Console.KeyAvailable) end = true;
            Thread.Sleep(1000);
        }

        test.Stop();
    }
}

public class UDPReceiver
{
    public void Init()
    {
        // compose IP address and port of local end point
        IPAddress.TryParse("0.0.0.0", out IPAddress ip);
        int.TryParse("33021", out int port);
        IPEndPoint LocalEndPoint = new IPEndPoint(ip, port);

        // create UdpClient
        C = new UdpClient(LocalEndPoint);

        // join multicast group
        IPAddress.TryParse("239.255.20.1", out IPAddress multicast);
        C.JoinMulticastGroup(multicast);

        // set socket options
        C.Client.SetSocketOption(SocketOptionLevel.Socket,
                                 SocketOptionName.ReceiveTimeout, 500);
        C.Client.SetSocketOption(SocketOptionLevel.Socket,
                                 SocketOptionName.ReceiveBuffer, 10000000);
    }

    public void Processing()
    {
        Last = DateTime.UtcNow;
        while (!Cancel)
        {
            try
            {
                // if data is available, receive it
                if (C.Available > 0)
                {
                    // temp variable for endpoint from which the datagram was received 
                    IPEndPoint endPoint = new IPEndPoint(new IPAddress(0), 0);

                    // receive a message 
                    Byte[] data = C.Receive(ref endPoint);
                    DateTime rcvTime = DateTime.UtcNow;

                    // test output
                    if (rcvTime > Last + TimeSpan.FromMilliseconds(1000))
                    {
                        Console.WriteLine($"----------- transmission gap of {(rcvTime-Last).TotalSeconds:F2} secs detected ------------");
                    }
                    Console.WriteLine($"msg of {data.Length} bytes received at {rcvTime:HH:mm:ss.fff}");
                    Last = rcvTime;
                }
                // if not, sleep a bit
                else
                {
                    Thread.Sleep(5);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"exception caught in worker thread: {ex}");
            }
        }
    }

    public void Start()
    {
        Worker = Task.Run(Processing);
    }

    public void Stop()
    {
        Cancel = true;
        Worker.Wait();
    }

    private Task Worker { get; set; } = null;
    private UdpClient C { get; set; } = null;
    private bool Cancel { get; set; } = false;
    private DateTime Last { get; set; }
}

I tried to receive the UDP multicast data stream in parallel on two computers and logged when the transmission gaps of 1...30 seconds happen. In the log file I can see a similar pattern on both computers.

I developed a dummy message sender that sends out multicast UDP messages with a message counter and some dummy text, message size approx. 90 bytes, message rate 100 messages in bursts each 100msec. In this case >500.000 messages were successfully received without a single message loss.

Why is the UDP multicast transmission dependent on the message content - binary messages don't work, text messages work well.

Is the UdpClient somehow analysing the content of the messages and discarding some?

I tried as well to switch off the firewall temporarily, but that as well had no effect.

The effect does not only happen in my test setup (two desktop computers in the same LAN as sender and receiver) but as well in the productive environment.

Meanwhile we tested the code above as well under Linux with the same effect. The operating system seems not to be the problem, is there a bug in .NET 8?

c# udp .net-8.0 multicast windows-11