首页 > 解决方案 > 如何在 Windows 10 中枚举 mDNS 服务?

问题描述

我正在尝试DeviceInformation.CreateWatcherUWP应用程序中使用来枚举mDNS服务:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    if (_dnsWatcher == null)
    {
        _dnsWatcher = DeviceInformation.CreateWatcher("(System.Devices.Aep.ProtocolId:=\"{4526e8c1-8aac-4153-9b16-55e86ada0e54}\")");
        _dnsWatcher.Added += DnsWatcher_Added;
        _dnsWatcher.EnumerationCompleted += DnsWatcher_EnumerationCompleted;
        _dnsWatcher.Removed += DnsWatcher_Removed;
        _dnsWatcher.Stopped += DnsWatcher_Stopped;
        _dnsWatcher.Updated += DnsWatcher_Updated;
        _dnsWatcher.Start();
    }
}

Wireshark显示没有mDNS广播,只有EnumerationCompleted回调发生。

我错过了什么吗?

标签: uwpmdns

解决方案


您需要添加您正在观看的内容。这是我的工作代码:

            var proto = "_gopher._tcp"; // e.g. _http._tcp
        var queryString = $"System.Devices.AepService.ProtocolId:={{{DnsSdProtocol}}} AND System.Devices.Dnssd.ServiceName:=\"{proto}\" AND System.Devices.Dnssd.Domain:=\"local\"";

        var askFor = new String[] { "System.Devices.Dnssd.HostName",
                            "System.Devices.Dnssd.ServiceName",
                            "System.Devices.Dnssd.InstanceName",
                            "System.Devices.IpAddress",
                            "System.Devices.Dnssd.PortNumber",
                            "System.Devices.Dnssd.TextAttributes",
                            };
        dw = DeviceInformation.CreateWatcher(queryString, askFor, DeviceInformationKind.AssociationEndpointService);

请注意,我的查询字符串包含一个用于搜索 (_gopher._tcp) 的协议和一个 dns 域。警告:我是 DNS-SD 的新手,我不知道哪些 dns 域设置有效。


推荐阅读