15

I created a small program to test UPnP Multicast (Visual C# 2010 Express, running on Windows 7 Professional 64 Bit). I can receive the UPnP NOTIFY Messages from UPnP Devices in my Network. But when i send the M-SEARCH Message, i get no Answers.

I have tested the same code on a iOS environment (Monotouch for iOS, running on a iPhone simulator on a Mac). There it runs fine and i get all the search responses from my UPnP devices. I can also see the M-SEARCH message from my windows program.

It looks like Windows (or a Firewall?) is hiding the search responses. Any idea?

Here is the code:

IPEndPoint LocalEndPoint = new IPEndPoint(IPAddress.Any, 1900);
IPEndPoint MulticastEndPoint = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);

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

UdpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
UdpSocket.Bind(LocalEndPoint);
UdpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(MulticastEndPoint.Address, IPAddress.Any));
UdpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 2);
UdpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, true);

Console.WriteLine("UDP-Socket setup done...\r\n");

string SearchString = "M-SEARCH * HTTP/1.1\r\nHOST:239.255.255.250:1900\r\nMAN:\"ssdp:discover\"\r\nST:ssdp:all\r\nMX:3\r\n\r\n";

UdpSocket.SendTo(Encoding.UTF8.GetBytes(SearchString), SocketFlags.None, MulticastEndPoint);

Console.WriteLine("M-Search sent...\r\n");

byte[] ReceiveBuffer = new byte[64000];

int ReceivedBytes = 0;

while (true)
{
    if (UdpSocket.Available > 0)
    {
        ReceivedBytes = UdpSocket.Receive(ReceiveBuffer, SocketFlags.None);

        if (ReceivedBytes > 0)
        {
            Console.WriteLine(Encoding.UTF8.GetString(ReceiveBuffer, 0, ReceivedBytes));
        }
    }
}
4

3 回答 3

19

是的,我解决了这个问题!小错误,大影响:

我的程序在绑定到 UPnP 多播组的端口 1900 上发送 M-SEARCH。因为我将 LocalEndPoint 绑定到同一个端口,UPnP 设备以单播方式响应端口 1900。在 iOS 上它可以工作,因为我的程序是唯一绑定到该端口的服务。但是在 PC 上,我发现了几个绑定到端口 1900 的服务(使用“netstat -p UDP -a”找到)。因此,来自 UPnP 设备的单播消息被其他服务之一吸收。

解决方案:我将 LocalEndPoint 绑定到一个空闲端口(例如 60000),现在它可以正常工作了!

IPEndPoint LocalEndPoint = new IPEndPoint(IPAddress.Any, 60000);
于 2012-10-10T08:46:54.900 回答
5

在创建本地端点时,使用端口 0(零)绑定不使用固定端口的空闲端口。又发现了一点。绑定 IPAddress.Any 或 IPAddress.Loopback 从 Microsoft(本地?)系统获取响应,其中绑定到 LAN 地址之一从本地网络获取响应。获取第一个 IPV4 地址可以这样完成:

IPAddress localNetwork = Dns.GetHostAddresses(Environment.GetEnvironmentVariable("COMPUTERNAME")).Where(ia => (ia.AddressFamily == AddressFamily.InterNetwork)).First();
于 2014-04-02T20:26:49.477 回答
1

对于后代:设置上述所有这些选项对于 M-SEARCH 来说是不必要的,甚至可能适得其反:

UdpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
UdpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(MulticastEndPoint.Address, IPAddress.Any));
UdpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 2);
UdpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, true);

所以不要这样做。

于 2016-03-02T22:37:02.420 回答