SSDP Search in Windows Phone 8

2019-06-06 16:54发布

问题:

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

回答1:

I'm not familiar with C# or dotnet APIs, but I can see some details wrong with the M-SEARCH message:

  • MAN header must be enclosed in double quotes, so MAN: "ssdp:discover"\r\n
  • MX header is missing (required for multicast)
  • USER-AGENT header is missing
  • missing an empty line in the end
  • Header names are supposedly case insensitive, but I'd use upper case just in case...

See the Device Architecture reference pdf for more details