2

Just trying to receive a UPnP broadcast from a device in my local network. I did find a lot of similar questions and tried a bunch of suggestions, none of theme where successful. I do see the UDP packets with Wireshark, so they are actually received on my computer. Any suggestions?

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class UDPListener
{
    private const int listenPort = 1900;

    private static void StartListener()
    {
        bool done = false;

        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, listenPort);

        UdpClient listener = new UdpClient();
        listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);        
        listener.Client.Bind(localEndPoint);
        listener.JoinMulticastGroup(IPAddress.Parse("239.255.255.250"));
        listener.MulticastLoopback = true;

        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 0);

        try
        {
            while (!done)
            {
                Console.WriteLine("Waiting for broadcast");
                var bytes = listener.Receive(ref groupEP);

                Console.WriteLine("Received broadcast from {0} :\n {1}\n",
                   groupEP.ToString(),
                   Encoding.ASCII.GetString(bytes, 0, bytes.Length));

            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            listener.Close();
        }
    }

    public static int Main()
    {
        StartListener();

        return 0;
    }
}
Stijn Van Antwerpen
  • 1,840
  • 17
  • 42
  • Take a look at this, maybe it will help you: http://stackoverflow.com/questions/12794761/upnp-multicast-missing-answers-from-m-search-discovery – Gusman Mar 04 '16 at 10:13

1 Answers1

5

Thanks to Try and Error I did got it working by specifying my local address while binding to the multicast group. I hardcoded the address in the example since It's is just a sandbox application. Using IPAddress.Any does not work. I don't know exactly why. For future reference and other poor souls who might looking for similar stuff:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class UDPListener
{
    private static void StartListener()
    {
        bool done = false;

        IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 1900);

        UdpClient listener = new UdpClient();
        listener.Client.Bind(localEndPoint);
        listener.JoinMulticastGroup(IPAddress.Parse("239.255.255.250"), IPAddress.Parse("10.32.4.129"));

        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 0);

        try
        {
            while (!done)
            {
                Console.WriteLine("Waiting for broadcast");
                var bytes = listener.Receive(ref groupEP);

                Console.WriteLine("Received broadcast from {0} :\n {1}\n",
                   groupEP.ToString(),
                   Encoding.ASCII.GetString(bytes, 0, bytes.Length));
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            listener.Close();
        }
    }

    public static int Main()
    {
        StartListener();

        return 0;
    }
}
Stijn Van Antwerpen
  • 1,840
  • 17
  • 42
  • 1
    (I know this is old, but it's still relevant) Thank you so much! It seems to be the port 1900 in `localEndPoint` that did it for me. – SWdV Dec 28 '18 at 17:24
  • 1
    `localEndPoint` better to use `0` as a port instead of `1900` because in some cases you can't reuse the `1900` if it is already busy – Yury Schkatula Feb 26 '19 at 22:17