1# MDNS Management 2 3## Overview 4 5Multicast DNS (MDNS) provides functions such as adding, removing, discovering, and resolving local services on a LAN. 6- Local service: a service provider on a LAN, for example, a printer or scanner. 7 8Typical MDNS management scenarios include: 9 10- Managing local services on a LAN, such as adding, removing, and resolving local services. 11- Discovering local services and listening to the status changes of local services of the specified type through the **DiscoveryService** object. 12 13> **NOTE** 14> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the callback mode. For details about the APIs, see [MDNS Management](../reference/apis/js-apis-net-mdns.md). 15 16The following describes the development procedure specific to each application scenario. 17 18## Available APIs 19 20For the complete list of JS APIs and example code, see, see [MDNS Management](../reference/apis/js-apis-net-mdns.md). 21 22| API | Description| 23| ----------------------- | ---- | 24| addLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void | Adds an MDNS service. This API uses an asynchronous callback to return the result.| 25| removeLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void | Removes an MDNS service. This API uses an asynchronous callback to return the result.| 26| createDiscoveryService(context: Context, serviceType: string): DiscoveryService | Creates a **DiscoveryService** object, which is used to discover MDNS services of the specified type.| 27| resolveLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void | Resolves an MDNS service. This API uses an asynchronous callback to return the result.| 28| startSearchingMDNS(): void | Searches for MDNS services on the LAN.| 29| stopSearchingMDNS(): void | Stops searching for MDNS services on the LAN.| 30| on(type: 'discoveryStart', callback: Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}>): void | Enables listening for **discoveryStart** events.| 31| off(type: 'discoveryStart', callback?: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void | Disables listening for **discoveryStart** events.| 32| on(type: 'discoveryStop', callback: Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}>): void | Enables listening for **discoveryStop** events.| 33| off(type: 'discoveryStop', callback?: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void | Disables listening for **discoveryStop** events.| 34| on(type: 'serviceFound', callback: Callback\<LocalServiceInfo>): void | Enables listening for **serviceFound** events.| 35| off(type: 'serviceFound', callback?: Callback\<LocalServiceInfo>): void | Disables listening for **serviceFound** events.| 36| on(type: 'serviceLost', callback: Callback\<LocalServiceInfo>): void | Enables listening for **serviceLost** events.| 37| off(type: 'serviceLost', callback?: Callback\<LocalServiceInfo>): void | Disables listening for **serviceLost** events.| 38 39## Managing Local Services 40 411. Connect the device to the Wi-Fi network. 422. Import the **mdns** namespace from **@ohos.net.mdns**. 433. Call **addLocalService** to add a local service. 444. (Optional) Call **resolveLocalService** to resolve the local service for the IP address of the local network. 455. Call **removeLocalService** to remove the local service. 46 47```ts 48// Import the mdns namespace from @ohos.net.mdns. 49import mdns from '@ohos.net.mdns'; 50import { BusinessError } from '@ohos.base'; 51import featureAbility from '@ohos.ability.featureAbility'; 52 53let context = getContext(this) as Context; 54 55class ServiceAttribute { 56 key: string = "111" 57 value: Array<number> = [1] 58} 59 60// Create a LocalService object. 61let localServiceInfo: mdns.LocalServiceInfo = { 62 serviceType: "_print._tcp", 63 serviceName: "servicename", 64 port: 5555, 65 host: { 66 address: "10.14.**.***" 67 }, 68 serviceAttribute: [{key: "111", value: [1]}] 69} 70 71// Call addLocalService to add a local service. 72mdns.addLocalService(context, localServiceInfo, (error: BusinessError, data: mdns.LocalServiceInfo) => { 73 console.log(JSON.stringify(error)); 74 console.log(JSON.stringify(data)); 75}); 76 77// (Optional) Call resolveLocalService to resolve the local service. 78mdns.resolveLocalService(context, localServiceInfo, (error: BusinessError, data: mdns.LocalServiceInfo) => { 79 console.log(JSON.stringify(error)); 80 console.log(JSON.stringify(data)); 81}); 82 83// Call removeLocalService to remove the local service. 84mdns.removeLocalService(context, localServiceInfo, (error: BusinessError, data: mdns.LocalServiceInfo) => { 85 console.log(JSON.stringify(error)); 86 console.log(JSON.stringify(data)); 87}); 88``` 89 90## Discovering Local Services 91 921. Connect the device to the Wi-Fi network. 932. Import the **mdns** namespace from **@ohos.net.mdns**. 943. Create a **DiscoveryService** object, which is used to discover MDNS services of the specified type. 954. Subscribe to MDNS service discovery status changes. 965. Enable discovery of MDNS services on the LAN. 976. Stop searching for MDNS services on the LAN. 987. Unsubscribe from MDNS service discovery status changes. 99 100```ts 101// Import the mdns namespace from @ohos.net.mdns. 102import mdns from '@ohos.net.mdns'; 103import UIAbility from '@ohos.app.ability.UIAbility'; 104import { BusinessError } from '@ohos.base'; 105import featureAbility from '@ohos.ability.featureAbility'; 106import window from '@ohos.window'; 107 108// Construct a singleton object. 109export class GlobalContext { 110 private constructor() {} 111 private static instance: GlobalContext; 112 private _objects = new Map<string, Object>(); 113 114 public static getContext(): GlobalContext { 115 if (!GlobalContext.instance) { 116 GlobalContext.instance = new GlobalContext(); 117 } 118 return GlobalContext.instance; 119 } 120 121 getObject(value: string): Object | undefined { 122 return this._objects.get(value); 123 } 124 125 setObject(key: string, objectClass: Object): void { 126 this._objects.set(key, objectClass); 127 } 128} 129 130// Obtain the context of the stage model. 131class EntryAbility extends UIAbility { 132 value:number = 0; 133 onWindowStageCreate(windowStage: window.WindowStage): void{ 134 GlobalContext.getContext().setObject("value", this.value); 135 } 136} 137 138let context = GlobalContext.getContext().getObject("value"); 139 140// Create a **DiscoveryService** object, which is used to discover MDNS services of the specified type. 141let serviceType = "_print._tcp"; 142let discoveryService = mdns.createDiscoveryService(context, serviceType); 143 144class DataServiceInfo{ 145 serviceInfo: mdns.LocalServiceInfo|null = null 146 errorCode?: mdns.MdnsError = mdns.MdnsError.INTERNAL_ERROR 147} 148// Subscribe to MDNS service discovery status changes. 149discoveryService.on('discoveryStart', (data: DataServiceInfo) => { 150 console.log(JSON.stringify(data)); 151}); 152discoveryService.on('discoveryStop', (data: DataServiceInfo) => { 153 console.log(JSON.stringify(data)); 154}); 155discoveryService.on('serviceFound', (data: mdns.LocalServiceInfo) => { 156 console.log(JSON.stringify(data)); 157}); 158discoveryService.on('serviceLost', (data: mdns.LocalServiceInfo) => { 159 console.log(JSON.stringify(data)); 160}); 161 162// Enable discovery of MDNS services on the LAN. 163discoveryService.startSearchingMDNS(); 164 165// Stop searching for MDNS services on the LAN. 166discoveryService.stopSearchingMDNS(); 167 168// Unsubscribe from MDNS service discovery status changes. 169discoveryService.off('discoveryStart', (data: DataServiceInfo) => { 170 console.log(JSON.stringify(data)); 171}); 172discoveryService.off('discoveryStop', (data: DataServiceInfo) => { 173 console.log(JSON.stringify(data)); 174}); 175discoveryService.off('serviceFound', (data: mdns.LocalServiceInfo) => { 176 console.log(JSON.stringify(data)); 177}); 178discoveryService.off('serviceLost', (data: mdns.LocalServiceInfo) => { 179 console.log(JSON.stringify(data)); 180}); 181``` 182