【问题标题】:Listen to UPnP Broadcast with UdpClient使用 UdpClient 收听 UPnP 广播
【发布时间】:2016-03-04 10:00:26
【问题描述】:

只是尝试从本地网络中的设备接收 UPnP 广播。我确实发现了很多类似的问题并尝试了很多建议,但没有一个主题成功。我确实看到了 Wireshark 的 UDP 数据包,所以它们实际上是在我的计算机上收到的。有什么建议?

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;
    }
}

【问题讨论】:

标签: c# broadcast upnp


【解决方案1】:

感谢 Try and Error 我确实通过在绑定到多播组时指定我的本地地址使其正常工作。我在示例中对地址进行了硬编码,因为它只是一个沙盒应用程序。使用IPAddress.Any 不起作用。我不知道具体为什么。供将来参考和其他可能正在寻找类似东西的可怜人:

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;
    }
}

【讨论】:

  • (我知道这是旧的,但它仍然相关)非常感谢!似乎是 localEndPoint 中的端口 1900 为我做的。
  • localEndPoint 最好使用0 作为端口而不是1900,因为在某些情况下,如果1900 已经很忙,您将无法重用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 2020-10-07
  • 2014-09-04
相关资源
最近更新 更多