3

我正在尝试使用C#为Windows Store应用程序实现基本的SSDP ( UDP ) 广播/侦听器。

我发现它Windows.Networking.Sockets包含DatagramSocket我需要用于UDP网络的类。

但是,我目前的尝试似乎执行得很好,但通过Wireshark没有结果,也没有从网络上的设备得到响应。

这是我目前正在使用的代码(并通过 RT Simulator 运行):

public async static Task<IEnumerable<HueBridge>> DiscoverAsync(TimeSpan timeout)
{
  if (timeout <= TimeSpan.Zero)
    throw new ArgumentException("Timeout value must be greater than zero.", "timeout");

  var discoveredBridges = new List<HueBridge>();

  using (var socket = new DatagramSocket())
  {
    while (true)
    {
      var bridgeWasFound = false;

      socket.MessageReceived += (sender, e) =>
      {
        var bpx = true; // breakpoint here for success
      };

      var multicastIP = new HostName("239.255.255.250");
      await socket.BindServiceNameAsync("1900");
      socket.JoinMulticastGroup(multicastIP);

      using (var writer = new DataWriter(socket.OutputStream))
      {
        var request = new StringBuilder();
        request.AppendLine("M-SEARCH * HTTP/1.1");
        request.AppendLine("HOST: 239.255.255.250:1900");
        request.AppendLine("MAN: ssdp:discover");
        request.AppendLine("MX: 5");
        request.AppendLine("ST: ssdp:all");

        writer.WriteString(request.ToString());
        await writer.FlushAsync();
      }

      if (timeout > TimeSpan.Zero)
        await Task.Delay(timeout);

      if (!bridgeWasFound)
        break; // breakpoint here for failure check
    }
  }

  return discoveredBridges;
}

关于我可能做错了什么的任何想法?我没有遇到异常,并且我在清单中设置了正确的功能。我的断点break总是被击中,我正在使用 10 秒的超时。

4

2 回答 2

3

似乎我发现了问题。

首先,我应该使用socket.BindEndpointAsync(null, string.Empty)而不是socket.BindServiceNameAsync("1900"),它将正确监听广播数据包。

其次,writer.FlushAsync()不写入套接字;但是,writer.StoreAsync()确实如此。

这是最终结果,它确实(几乎)完美地工作:

public async static Task<IEnumerable<HueBridge>> DiscoverAsync(TimeSpan timeout)
{
  if (timeout <= TimeSpan.Zero)
    throw new ArgumentException("Timeout value must be greater than zero.", "timeout");

  var discoveredBridges = new List<HueBridge>();
  var multicastIP = new HostName("239.255.255.250");
  var bridgeWasFound = false;

  using (var socket = new DatagramSocket())
  {
    socket.MessageReceived += (sender, e) =>
    {
      var reader = e.GetDataReader();
      var bytesRemaining = reader.UnconsumedBufferLength;
      var receivedString = reader.ReadString(bytesRemaining);

      // TODO: Check for existing bridges, only add new ones to prevent infinite loop.
      // TODO: Create new bridge and add to the list. 

      bridgeWasFound = true;
    };

    await socket.BindEndpointAsync(null, string.Empty);
    socket.JoinMulticastGroup(multicastIP);

    while (true)
    {
      bridgeWasFound = false;

      using (var stream = await socket.GetOutputStreamAsync(multicastIP, "1900"))
      using (var writer = new DataWriter(stream))
      {
        var request = new StringBuilder();
        request.AppendLine("M-SEARCH * HTTP/1.1");
        request.AppendLine("HOST: 239.255.255.250:1900");
        request.AppendLine("MAN: ssdp:discover");
        request.AppendLine("MX: 3");
        request.AppendLine("ST: ssdp:all");

        writer.WriteString(request.ToString());
        await writer.StoreAsync();

        if (timeout > TimeSpan.Zero)
          await Task.Delay(timeout);

        if (!bridgeWasFound)
          break;
      }
    }
  }

  return discoveredBridges;
}
于 2013-01-11T22:32:58.927 回答
1

根据规格:

HTTP 扩展框架需要 MAN。与 NTS 和 ST 字段值不同,MAN 头字段的字段值用双引号括起来;它定义了扩展的范围(命名空间)。必须是“ssdp:discover”。

然后你的代码

request.AppendLine("MAN: ssdp:discover");

一定是

request.AppendLine("MAN: \"ssdp:discover\"");

希望这有帮助。

于 2013-02-15T07:59:34.977 回答