0

Is UDP Multicast disabled by default on Raspbian?

Probed with netstat -g and raspbian is joining multicast group.

Probed with tcpdump but can't receive packets.

When I send from a Raspberry, I can receive on a Windows 10 machine, but the opposite I can't.

When I try to listen and receive on Raspberry, I don't receive any packet.

How can I enable multicast receiving on Raspberry/Raspbian?

  • What multicast you are looking for? UPnP/DLNA or igmp queries or grouped audio/video streams or mDNS or what? Do you see anywhere UDP packages with ip address 239.255.255.250 and port 1900? Is there igmp snooping enabled? – Ingo Apr 11 at 10:45
  • I ended up solving this issue, the problem was with my example. – user116046 Apr 11 at 20:30
  • Please make an answer for your solution and accept it after two days. Or just delete your question. Otherwise it will annoying us for years to pop up again and ask for a solution. Only accepting an answer will finish the question. Thank you :-) – Ingo Apr 11 at 20:51
  • OK, I'll post it. – user116046 Apr 12 at 1:34
  • I will upvote it when accepted. – Ingo Apr 12 at 8:11
0

I followed this example (dotnet Core):

https://docs.microsoft.com/pt-br/dotnet/api/system.net.sockets.multicastoption?view=netframework-4.8

But Raspbian can't receive messages.

When the program asks for local IP, change the code to IPAddress.Any (it simply works don't know why).

Be sure to check with ip a if multicast is enabled:

$ ip a

...           -> This option 
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>
...

I didn't had to add any routes. (There might be some problems with USB nano wifi receivers ).

In Raspbian I can send as a regular user but I can receive only with root rights. (maybe because I binded the socket to IPAdress.Any).

I simplified the example a bit:

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

namespace MultiCastTestCSharp
{
    class Program
    {
        static string multicastIP = "239.0.0.10";
        static string multicastPort = "123";
        static void Main(string[] args)
        {
            bool done = false;
            while (!done)
            {
                Console.WriteLine("Sender[1] Receiver [2] Exit [3]:");
                var input = Console.ReadLine();
                if (input == "1")
                {
                    RunAsSender();
                    Environment.Exit(0);
                }
                else if (input == "2")
                {
                    RunAsListener();
                    Environment.Exit(0);
                }
                else if (input == "3")
                {
                    Environment.Exit(0);
                }
            }
        }

        static void RunAsSender()
        {
            IPAddress mcastAddress = IPAddress.Parse(multicastIP);
            int mcastPort = int.Parse(multicastPort);
            Socket mcastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint endPoint = new IPEndPoint(mcastAddress, mcastPort);
            MulticastOption mcastOption = new MulticastOption(mcastAddress, IPAddress.Any);
            mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption);

            bool done = false;

            Console.WriteLine("Multicast group is: " + mcastOption.Group);
            Console.WriteLine("Multicast port is: " + mcastPort.ToString());
            Console.WriteLine("Multicast local address is: " + mcastOption.LocalAddress);

            Console.WriteLine("Sending multicast packets.......");

            Task.Run(() =>
            {
                Console.ReadLine();
                done = true;
            });

            int n = 0;

            mcastSocket.Connect(endPoint);

            while (!done)
            {
                mcastSocket.Send(Encoding.ASCII.GetBytes(DateTime.Now.ToString()));
                Console.Write($"\r{n++} Messages sent.           ");
                Thread.Sleep(1000);
            }
            mcastSocket.Close();
        }
        static void RunAsListener()
        {
            IPAddress multicastIP = IPAddress.Parse(Program.multicastIP);
            int multicastPort = int.Parse(Program.multicastPort);
            IPEndPoint localEP = new IPEndPoint(IPAddress.Any, multicastPort);

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

            MulticastOption option = new MulticastOption(multicastIP);
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, option);

            socket.Bind(localEP);

            bool done = false;
            byte[] bytes = new byte[100];

            Console.WriteLine("Multicast group is: " + option.Group);
            Console.WriteLine("Multicast port is: " + multicastPort.ToString());
            Console.WriteLine("Multicast local address is: " + option.LocalAddress);
            Console.WriteLine("Waiting for multicast packets.......");

            Task.Run(() =>
            {
                Console.ReadLine();
                done = true;
            });

            while (!done)
            {
                socket.Receive(bytes);
                Console.WriteLine("Received broadcast from {0} :\n  {1}\n", multicastIP.ToString(), Encoding.ASCII.GetString(bytes, 0, bytes.Length));
            }

            socket.Close();
        }
    }
}
| improve this answer | |
  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year. – Ingo Apr 14 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy