async void registerservice() { // create a listening socket to watch for incoming connections from...
TRANSCRIPT
Jay MahendruProgram ManagerDNS-SD API
Discovering Devices or Services over the Local Network
3-79
Agenda
What is DNS-SD?RegistrationDiscovery & ResolutionConnectionCode SampleResources
• Uses standard DNS records to register and discover other services (device or app) on the local network
• Works in conjunction with mDNS, allowing devices to communicate with each other, without the need for a unicast server
DNS Service Discovery (DNS-SD)
DNS-SD enables easy discovery of other services on the local network
• Advertise app to others on the network• Service Name Format: <Instance Name>.<Service
Type>.<Domain>• Instance Name: Human-readable name (such as “Simple Chat
App”)• Service Type: Combination of the protocol name + transport(such
as _sca._tcp)• Domain: Standard DNS domain, such as microsoft.com
• Can also specify text attributes:• Max Concurrent Users; 20• Locale; En-US
Registration
• Are there any instances of _sca._tcp on the local domain?• Yes:
1. JayM._sca._tcp.local2. KaylenW._sca._tcp.local
• Resolve:1. 124.169.26.15 (IP), 1010 (Port); Max Concurrent Users = 20; Locale
= En-US2. 124.169.26.16 (IP), 1010 (Port); Max Concurrent Users = 20; Locale
= En-US
Discovery & Resolution
• Use the resolved port number and hostname for connecting to the app• Can use a number of methods such as the
Modern Sockets API
Connect
async void RegisterService() { // Create a listening socket to watch for incoming connections from other apps StreamSocketListener listener = new StreamSocketListener();
// We can pass an actual port number here or one will get assigned automatically await listener.BindServiceNameAsync("");
// Create a DNS-SD Service Instance DnssdServiceInstance chatServ = new DnssdServiceInstance("SimpleChatApp._sca._tcp.local.", null);
// Can add optional text attributes (key-val pairs) as well chatServ.TextAttributes.Add("MaxConcurrentUsers", "20"); chatServ.TextAttributes.Add("Locale", "En-US");
// Register Service var registrationResult = await chatServ.RegisterStreamSocketListenerAsync(listener);}
Sample Code – Service Registration
Sample Code – Service Discovery & Resolution• Use the Windows.Devices.Enumeration API to discover the DNS-SD services
// Devices.Enumeration API requires a GUID for each discovery type (such as DNS-SD, Bluetooth etc.)public static Guid DnsSdProtocol = new Guid("{4526e8c1-8aac-4153-9b16-55e86ada0e54}");
// Filter results by domain and service namestring queryString = "System.Devices.AepService.ProtocolId:={" + DnsSdProtocol + "} AND " + "System.Devices.Dnssd.Domain:=\"local\" AND System.Devices.Dnssd.ServiceName:=\"_sca._tcp\"";
// Start a watcher with the query string, and request other properties (discover & resolve)var watcher = DeviceInformation.CreateWatcher(queryString, new String[] { "System.Devices.Dnssd.HostName", "System.Devices.Dnssd.ServiceName", "System.Devices.Dnssd.TextAttributes", "System.Devices.IpAddress"}, DeviceInformationKind.AssociationEndpointService);
// Add callback to watcher watcher.Added += ConnectToService;
watcher.Start();
Sample Code – Connectivityasync void ConnectToService(DeviceWatcher sender, DeviceInformation args){ // The IpAddress property contains a list of strings representing IP literals var ipAddresses = args["System.Devices.IpAddress"] as string[]; var remoteAddress = new HostName(ipAddresses[0]);
// The PortNumber property gives us the port on which the service is advertised (UInt16) var remotePort = args["System.Devices.Dnssd.PortNumber"]. ToString(); var streamSock = new StreamSocket();
// The TextAttributes property contains a list of the text attributes added, such as // Max Concurrent Users = 20; Locale = En-US var textAttrs = args["TextAttributes"] as string[];
// Connect to the other app instance await streamSock.ConnectAsync(remoteAddress, remotePort);}
• MSDN Documentation & Sample App:• http://aka.ms/dnssdapi• Check out the sample app and create your own service discovery app!
Resources
© 2015 Microsoft Corporation. All rights reserved.