1

I'm new at SSDP/UPNP/Sockets and all that jazz. I'm playing around with it a bit and I just want to see what a generic SSDP search on my network will bring up.

Using this SSDP Sniffer app, I get a lot of results so I'm attempting to recreate this.

I'm using the following code, which I've found various versions of, but all the tweaking I do doesn't appear to bring back any results. I pretty much at a loss here and would appreciate any guidance.

thanks!

private const string SSDP_IP = "239.255.255.250";
private const string SSDP_PORT = "1900";
private const string SSDP_QUERY = "M-SEARCH * HTTP/1.1\r\n" +
                                  "Host: " + SSDP_IP + ":" + SSDP_PORT + "\r\n" +
                                  "Man: ssdp:discover\r\n" +
                                  "ST: ssdp:all\r\n";

DataGramSocket socket;

async public void SsdpQueryAsync()
{
    var remoteIP = new Windows.Networking.HostName(SSDP_IP);
    var reqBuff = Encoding.UTF8.GetBytes(SSDP_QUERY);

    socket = new DatagramSocket();

    socket.MessageReceived += (sender, args) =>
    {
        // This is invoked for each device that responds to the query...
        Task.Run(() =>
            {
                 // do something useful
            });
    };

    await socket.BindEndpointAsync(null, "");

    socket.JoinMulticastGroup(remoteIP);

    using (var stream = await socket.GetOutputStreamAsync(remoteIP, SSDP_PORT))
    {
        await stream.WriteAsync(reqBuff.AsBuffer());
    }

    await Task.Delay(5000);
}
4

1 回答 1

2

我不熟悉 C# 或 dotnet API,但我可以看到 M-SEARCH 消息中的一些细节错误:

  • MAN 标头必须用双引号引起来,所以MAN: "ssdp:discover"\r\n
  • MX 标头丢失(多播所需)
  • 缺少 USER-AGENT 标头
  • 最后缺少一个空行
  • 标题名称应该不区分大小写,但我会使用大写以防万一......

有关详细信息,请参阅设备架构参考 pdf

于 2013-09-18T11:23:36.473 回答