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