为什么我不能让UPnP单播M-SEARCH工作而不是MultiCast M-SEARCH?

时间:2015-03-05 05:37:41

标签: udp multicast upnp udpclient

早上好,

我们决定尽可能多地使用UPnP。我们在239.255.255.250:1900上使用MultiCast进行M-SEARCH。

但是,我们正在研究如何在客户将MultiCast锁定在其网络上时进行处理。看看UPnP 1.1规范,它谈到了使用M-SEARCH的单播。因此,如果我们已经知道了我们想要与之交谈的各种设备的IP地址,并且他们正在收听0.0.0.0:1900,我们认为我们可以向deviceIP上的每个设备发送单播M-SEARCH: 1900。

我一直在努力做到这一点,并且正好让设备接收并响应单播M-SEARCH请求。

首先,是否允许您使用单播M-SEARCH启动设备的第一次UPnP对话?

第二,是否有理由倾听0.0.0.0:1900不接受发送到deviceIP的消息:1900?

当我在我的机器上执行netstat以查看正在使用的IP和端口时,似乎239.255.255.250:1900不在列表中,或者它显示为0.0.0.0:1900。 p>

因此,如果0.0.0.0是(ANY_IP)那么只有一个监听器在0.0.0.0:1900上监听就足以接收任何消息MultiCast到239.255.255.250:1900以及任何直接通过单播发送到该机器的消息#39 ; s ip:1900?

在测试时,我能够始终接收MultiCasts,但我从未收到过M-SEARCH的单播。在进行GET等时,我能够与其他端口上的设备进行通信,但似乎我无法通过端口1900响应单播M-SEARCH。

你真的可以在239.255.255.250:1900上收听多播,并在没有udp套接字冲突的情况下同时在同一台机器上以0.000.0:1900单播监听吗?

对此的任何建议和指示都将不胜感激。

谢谢, 柯蒂斯

PS:我使用的代码如下。对于构造函数中的地址,我们传入IPAddress.Any(0.0.0.0)和Protocol.Port是1900.这是在Windows 8.1下的Windows机器上运行的:

//
// SsdpSocket.cs
//
// Author:
//   Aaron Bockover <abockover@novell.com>
//
// Copyright (C) 2008 Novell, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

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

namespace Mono.Ssdp.Mono.Ssdp.Internal
{
    class SsdpSocket : Socket
    {
        static readonly IPEndPoint ssdp_send_point = new IPEndPoint (Protocol.IPAddress, Protocol.Port);

        readonly IPEndPoint ssdp_receive_point;

        public SsdpSocket (IPAddress address)
            : base (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
        {
            ssdp_receive_point = new IPEndPoint (address, Protocol.Port);
            SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        }

        public IAsyncResult BeginSendTo (byte [] data, AsyncCallback callback)
        {
            return BeginSendTo (data, callback, ssdp_send_point);
        }

        public IAsyncResult BeginSendTo (byte[] data, AsyncCallback callback, IPEndPoint endPoint)
        {
            return BeginSendTo (data, 0, data.Length, SocketFlags.None, endPoint, callback, this);
        }

        public IAsyncResult BeginReceiveFrom (AsyncReceiveBuffer buffer, AsyncCallback callback)
        {
            return base.BeginReceiveFrom (buffer.Buffer, 0, buffer.Buffer.Length, SocketFlags.None, 
                ref buffer.SenderEndPoint, callback, buffer);
        }

        public void Bind ()
        {
            Bind (ssdp_receive_point);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

解决此问题的方法是验证单播消息。

以下是两个消息示例。第一个是MultiCast,第二个是UniCast:

M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1900 
MAN: "ssdp:discover" 
MX: seconds to delay response 
ST: search target 
USER-AGENT: OS/version UPnP/1.1 product/version



M-SEARCH * HTTP/1.1
HOST: hostname:portNumber
MAN: "ssdp:discover"
ST: search target
USER-AGENT: OS/version UPnP/1.1 product/version

请注意,第二个M-SEARCH是单播搜索,不需要在其中包含“MX:”行。我使用的代码需要MX:行并使用其值。如果没有MX:行,我们会收到一个异常隐藏的异常并被catch(exception){}

吞噬

无论如何,UPnPServer只是听取0.0.0.0:1900并且会听到所有多重演员和单播的搜索。