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

早上好,

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

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

我一直在努力做到这一点,并且有一点时间让设备接收并响应单播M-SEARCH请求.

首先,是否允许您使用单播M-SEARCH进行首次UPnP对话?

第二,是否有一些理由认为0.0.0.0:1900上的监听不会接受发送到deviceIP:1900的消息?

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

那么,如果0.0.0.0是(ANY_IP)那么只有一个监听器在0.0.0.0:1900上监听足以接收任何消息MultiCast到239.255.255.250:1900并且任何直接通过单播发送到该机器的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);
        }
    }
}
最佳答案
解决此问题的方法是验证Unicast消息.

以下是两个消息示例.第一个是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:行,我们就会得到一个异常隐藏起来并被捕获(异常){}吞噬的异常

无论如何,UPnPServer只是在0.0.0.0:1900上收听,并且会听到所有的Multi-Cast和Unicast的搜索.

转载注明原文:udp – 为什么我不能让UPnP单播M-SEARCH工作而不是MultiCast M-SEARCH? - 代码日志