• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.net.mdns (MDNS Management)
2
3Multicast DNS (MDNS) provides functions such as adding, removing, discovering, and resolving local services on a LAN.
4
5> **NOTE**
6> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
7
8## Modules to Import
9
10```ts
11import mdns from '@ohos.net.mdns'
12```
13
14## mdns.addLocalService
15
16addLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void
17
18Adds an MDNS service. This API uses an asynchronous callback to return the result.
19
20**System capability**: SystemCapability.Communication.NetManager.MDNS
21
22**Parameters**
23
24| Name       | Type                            | Mandatory| Description                                    |
25|-------------|----------------------------------|-----------|-------------------------------------------------|
26| context     | Context                          | Yes      | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-app-ability-uiAbility.md).|
27| serviceInfo | [LocalServiceInfo](#localserviceinfo)                 | Yes       |   MDNS service information.     |
28| callback | AsyncCallback\<[LocalServiceInfo](#localserviceinfo)> | Yes       |   Callback used to return the result. If the operation is successful, **error** is **undefined** and **data** is the MDNS service information.     |
29
30**Error codes**
31
32| ID     | Error Message|
33|---------|---|
34| 401     | Parameter error. |
35| 2100002 | Operation failed. Cannot connect to service. |
36| 2100003 | System internal error. |
37| 2204003 | Callback duplicated. |
38| 2204008 | Service instance duplicated. |
39| 2204010 | Send packet failed. |
40
41> **NOTE**
42> For details about the error codes, see [MDNS Error Codes](../errorcodes/errorcode-net-mdns.md).
43
44**Example**
45
46Stage model:
47
48```ts
49// Obtain the context.
50import mdns from '@ohos.net.mdns'
51import { BusinessError } from '@ohos.base';
52
53let context = getContext(this) as Context;
54
55let localServiceInfo: mdns.LocalServiceInfo = {
56  serviceType: "_print._tcp",
57  serviceName: "servicename",
58  port: 5555,
59  host: {
60  address: "10.14.**.***",
61  },
62  serviceAttribute: [{key: "111", value: [1]}]
63}
64
65mdns.addLocalService(context, localServiceInfo, (error:BusinessError, data:mdns.LocalServiceInfo) =>  {
66  console.log(JSON.stringify(error));
67  console.log(JSON.stringify(data));
68});
69```
70
71## mdns.addLocalService
72
73addLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise\<LocalServiceInfo>
74
75Adds an MDNS service. This API uses a promise to return the result.
76
77**System capability**: SystemCapability.Communication.NetManager.MDNS
78
79**Parameters**
80
81| Name       | Type                            | Mandatory| Description                                    |
82|-------------|----------------------------------|-----------|-------------------------------------------------|
83| context     | Context                          | Yes      | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-app-ability-uiAbility.md).|
84| serviceInfo | [LocalServiceInfo](#localserviceinfo)                 | Yes       |   MDNS service information.     |
85
86**Return value**
87
88| Type                             | Description                                 |
89| --------------------------------- | ------------------------------------- |
90| Promise\<[LocalServiceInfo](#localserviceinfo)> | Promise used to return the result.|
91
92**Error codes**
93
94| ID     | Error Message|
95|---------|---|
96| 401     | Parameter error. |
97| 2100002 | Operation failed. Cannot connect to service. |
98| 2100003 | System internal error. |
99| 2204003 | Callback duplicated. |
100| 2204008 | Service instance duplicated. |
101| 2204010 | Send packet failed. |
102
103> **NOTE**
104> For details about the error codes, see [MDNS Error Codes](../errorcodes/errorcode-net-mdns.md).
105
106**Example**
107
108Stage model:
109
110```ts
111// Obtain the context.
112import mdns from '@ohos.net.mdns'
113import { BusinessError } from '@ohos.base';
114
115let context = getContext(this) as Context;
116
117let localServiceInfo: mdns.LocalServiceInfo = {
118  serviceType: "_print._tcp",
119  serviceName: "servicename",
120  port: 5555,
121  host: {
122    address: "10.14.**.***",
123  },
124  serviceAttribute: [{key: "111", value: [1]}]
125}
126
127mdns.addLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => {
128  console.log(JSON.stringify(data));
129});
130```
131
132## mdns.removeLocalService
133
134removeLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void
135
136Removes an MDNS service. This API uses an asynchronous callback to return the result.
137
138**System capability**: SystemCapability.Communication.NetManager.MDNS
139
140**Parameters**
141
142| Name       | Type                            | Mandatory| Description                                    |
143|-------------|----------------------------------|-----------|-------------------------------------------------|
144| context     | Context                          | Yes      | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-app-ability-uiAbility.md).|
145| serviceInfo | [LocalServiceInfo](#localserviceinfo)                 | Yes       |   MDNS service information.     |
146| callback | AsyncCallback\<[LocalServiceInfo](#localserviceinfo)> | Yes       |   Callback used to return the result. If the operation is successful, **error** is **undefined** and **data** is the MDNS service information.     |
147
148**Error codes**
149
150| ID     | Error Message|
151|---------|---|
152| 401     | Parameter error. |
153| 2100002 | Operation failed. Cannot connect to service. |
154| 2100003 | System internal error. |
155| 2204002 | Callback not found. |
156| 2204008 | Service instance not found. |
157| 2204010 | Send packet failed. |
158
159> **NOTE**
160> For details about the error codes, see [MDNS Error Codes](../errorcodes/errorcode-net-mdns.md).
161
162**Example**
163
164Stage model:
165
166```ts
167// Obtain the context.
168import mdns from '@ohos.net.mdns'
169import { BusinessError } from '@ohos.base';
170
171let context = getContext(this) as Context;
172
173let localServiceInfo: mdns.LocalServiceInfo = {
174  serviceType: "_print._tcp",
175  serviceName: "servicename",
176  port: 5555,
177  host: {
178  address: "10.14.**.***",
179  },
180  serviceAttribute: [{key: "111", value: [1]}]
181}
182
183mdns.removeLocalService(context, localServiceInfo, (error: BusinessError, data: mdns.LocalServiceInfo) =>  {
184  console.log(JSON.stringify(error));
185  console.log(JSON.stringify(data));
186});
187```
188
189## mdns.removeLocalService
190
191removeLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise\<LocalServiceInfo>
192
193Removes an MDNS service. This API uses a promise to return the result.
194
195**System capability**: SystemCapability.Communication.NetManager.MDNS
196
197**Parameters**
198
199| Name       | Type                            | Mandatory| Description                                    |
200|-------------|----------------------------------|-----------|-------------------------------------------------|
201| context     | Context                          | Yes      | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-app-ability-uiAbility.md).|
202| serviceInfo | [LocalServiceInfo](#localserviceinfo)                 | Yes       |   MDNS service information.     |
203
204**Return value**
205
206| Type                             | Description                                 |
207| --------------------------------- | ------------------------------------- |
208| Promise\<[LocalServiceInfo](#localserviceinfo)> | Promise used to return the result.|
209
210**Error codes**
211
212| ID     | Error Message|
213|---------|---|
214| 401     | Parameter error. |
215| 2100002 | Operation failed. Cannot connect to service. |
216| 2100003 | System internal error. |
217| 2204002 | Callback not found. |
218| 2204008 | Service instance not found. |
219| 2204010 | Send packet failed. |
220
221> **NOTE**
222> For details about the error codes, see [MDNS Error Codes](../errorcodes/errorcode-net-mdns.md).
223
224**Example**
225
226Stage model:
227
228```ts
229import mdns from '@ohos.net.mdns'
230import { BusinessError } from '@ohos.base';
231
232let context = getContext(this) as Context;
233
234let localServiceInfo: mdns.LocalServiceInfo = {
235  serviceType: "_print._tcp",
236  serviceName: "servicename",
237  port: 5555,
238  host: {
239  address: "10.14.**.***",
240  },
241  serviceAttribute: [{key: "111", value: [1]}]
242}
243
244mdns.removeLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => {
245  console.log(JSON.stringify(data));
246});
247```
248
249## mdns.createDiscoveryService
250
251createDiscoveryService(context: Context, serviceType: string): DiscoveryService
252
253Creates a **DiscoveryService** object, which is used to discover MDNS services of the specified type.
254
255**System capability**: SystemCapability.Communication.NetManager.MDNS
256
257**Parameters**
258
259| Name       | Type                            | Mandatory| Description                                    |
260|-------------|---------|-----------| ------------------------------------------------------------ |
261| context     | Context                          | Yes      | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-app-ability-uiAbility.md).|
262| serviceType | string  | Yes      | Type of the MDNS services to be discovered.|
263
264**Return value**
265
266| Type                         | Description                     |
267| ----------------------------- |---------------------------------|
268| DiscoveryService | **DiscoveryService** object used to discover MDNS services based on the specified **serviceType** and **Context**.|
269
270**Error codes**
271
272| ID     | Error Message|
273|---------|---|
274| 401     | Parameter error. |
275
276**Example**
277
278Stage model:
279
280```ts
281// Obtain the context.
282import mdns from '@ohos.net.mdns'
283import { BusinessError } from '@ohos.base';
284
285let context = getContext(this) as Context;
286
287let serviceType = "_print._tcp";
288let discoveryService : Object = mdns.createDiscoveryService(context, serviceType);
289```
290
291## mdns.resolveLocalService
292
293resolveLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void
294
295Resolves an MDNS service. This API uses an asynchronous callback to return the result.
296
297**System capability**: SystemCapability.Communication.NetManager.MDNS
298
299**Parameters**
300
301| Name       | Type                            | Mandatory| Description                                    |
302|-------------|----------------------------------|-----------|-------------------------------------------------------------|
303| context     | Context                          | Yes      | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-app-ability-uiAbility.md).|
304| serviceInfo | [LocalServiceInfo](#localserviceinfo)                 | Yes       |   MDNS service information.     |
305| callback | AsyncCallback\<[LocalServiceInfo](#localserviceinfo)> | Yes       |   Callback used to return the result. If the operation is successful, **error** is **undefined** and **data** is the MDNS service information.     |
306
307**Error codes**
308
309| ID     | Error Message|
310|---------|----------------------------------------------|
311| 401     | Parameter error.                             |
312| 2100002 | Operation failed. Cannot connect to service. |
313| 2100003 | System internal error.                       |
314| 2204003 | Callback duplicated.                         |
315| 2204006 | Request timeout.                |
316| 2204010 | Send packet failed.                          |
317
318> **NOTE**
319> For details about the error codes, see [MDNS Error Codes](../errorcodes/errorcode-net-mdns.md).
320
321**Example**
322
323Stage model:
324
325```ts
326// Obtain the context.
327import mdns from '@ohos.net.mdns'
328import { BusinessError } from '@ohos.base';
329
330let context = getContext(this) as Context;
331
332let localServiceInfo: mdns.LocalServiceInfo = {
333  serviceType: "_print._tcp",
334  serviceName: "servicename",
335  port: 5555,
336  host: {
337  address: "10.14.**.***",
338  },
339  serviceAttribute: [{key: "111", value: [1]}]
340}
341
342mdns.resolveLocalService(context, localServiceInfo, (error: BusinessError, data: mdns.LocalServiceInfo) =>  {
343  console.log(JSON.stringify(error));
344  console.log(JSON.stringify(data));
345});
346```
347
348## mdns.resolveLocalService
349
350resolveLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise\<LocalServiceInfo>
351
352Resolves an MDNS service. This API uses a promise to return the result.
353
354**System capability**: SystemCapability.Communication.NetManager.MDNS
355
356**Parameters**
357
358| Name       | Type                            | Mandatory| Description                                    |
359|-------------|--------------|-----------|-----------------------------------------------------|
360| context     | Context                          | Yes      | Application context.<br>For details about the application context of the FA model, see [Context](js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](js-apis-app-ability-uiAbility.md).|
361| serviceInfo | [LocalServiceInfo](#localserviceinfo)                 | Yes       |   MDNS service information.     |
362
363**Return value**
364
365| Type                             | Description                                 |
366|----------------------------| ------------------------------------- |
367| Promise\<[LocalServiceInfo](#localserviceinfo)> | Promise used to return the result.|
368
369**Error codes**
370
371| ID     | Error Message|
372|---------|----------------------------------------------|
373| 401     | Parameter error.                             |
374| 2100002 | Operation failed. Cannot connect to service. |
375| 2100003 | System internal error.                       |
376| 2204003 | Callback duplicated.                         |
377| 2204006 | Request timeout.                |
378| 2204010 | Send packet failed.                          |
379
380> **NOTE**
381> For details about the error codes, see [MDNS Error Codes](../errorcodes/errorcode-net-mdns.md).
382
383**Example**
384
385Stage model:
386
387```ts
388// Obtain the context.
389import mdns from '@ohos.net.mdns'
390import { BusinessError } from '@ohos.base';
391
392let context = getContext(this) as Context;
393
394let localServiceInfo: mdns.LocalServiceInfo = {
395  serviceType: "_print._tcp",
396  serviceName: "servicename",
397  port: 5555,
398  host: {
399  address: "10.14.**.***",
400  },
401  serviceAttribute: [{key: "111", value: [1]}]
402}
403
404mdns.resolveLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => {
405  console.log(JSON.stringify(data));
406});
407```
408## DiscoveryService
409
410Defines a **DiscoveryService** object for discovering MDNS services of the specified type.
411
412### startSearchingMDNS
413
414startSearchingMDNS(): void
415
416Searches for MDNS services on the LAN.
417
418**System capability**: SystemCapability.Communication.NetManager.MDNS
419
420**Example**
421
422Stage model:
423
424```ts
425// Obtain the context.
426import mdns from '@ohos.net.mdns'
427import { BusinessError } from '@ohos.base';
428
429let context = getContext(this) as Context;
430let serviceType = "_print._tcp";
431let discoveryService = mdns.createDiscoveryService(context, serviceType);
432discoveryService.startSearchingMDNS();
433```
434
435### stopSearchingMDNS
436
437stopSearchingMDNS(): void
438
439Stops searching for MDNS services on the LAN.
440
441**System capability**: SystemCapability.Communication.NetManager.MDNS
442
443**Example**
444
445Stage model:
446
447```ts
448// Obtain the context.
449import mdns from '@ohos.net.mdns'
450import { BusinessError } from '@ohos.base';
451
452let context = getContext(this) as Context;
453let serviceType = "_print._tcp";
454let discoveryService = mdns.createDiscoveryService(context, serviceType);
455discoveryService.stopSearchingMDNS();
456```
457
458### on('discoveryStart')
459
460on(type: 'discoveryStart', callback: Callback\<DiscoveryEventInfo\>): void
461
462Enables listening for **discoveryStart** events.
463
464**System capability**: SystemCapability.Communication.NetManager.MDNS
465
466**Parameters**
467
468| Name     | Type                            | Mandatory| Description                                                  |
469|-------------|---------------------------------|------|--------------------------------------------------------|
470| type        | string                          | Yes  | Event type. This field has a fixed value of **discoveryStart**.<br>**discoveryStart**: event of starting discovery of MDNS services on the LAN.|
471| callback    | Callback\<DiscoveryEventInfo\>  | Yes  | Callback used to return the MDNS service and error information.                          |
472
473**Example**
474
475```ts
476import mdns from '@ohos.net.mdns'
477import { BusinessError } from '@ohos.base';
478
479// See mdns.createDiscoveryService.
480let context = getContext(this) as Context;
481let serviceType = "_print._tcp";
482let discoveryService = mdns.createDiscoveryService(context, serviceType);
483discoveryService.startSearchingMDNS();
484
485discoveryService.on('discoveryStart', (data: mdns.DiscoveryEventInfo) => {
486  console.log(JSON.stringify(data));
487});
488
489discoveryService.stopSearchingMDNS();
490```
491
492### off('discoveryStart')
493
494off(type: 'discoveryStart', callback?: Callback\<DiscoveryEventInfo\>): void
495
496Disables listening for **discoveryStart** events.
497
498**System capability**: SystemCapability.Communication.NetManager.MDNS
499
500**Parameters**
501
502| Name       | Type                            | Mandatory| Description                                    |
503|-------------|--------------|-----------|-----------------------------------------------------|
504| type     | string                          | Yes      |Event type. This field has a fixed value of **discoveryStart**.<br>**discoveryStart**: event of starting discovery of MDNS services on the LAN.|
505| callback | Callback\<DiscoveryEventInfo\>  | No      |Callback used to return the MDNS service and error information.         |
506
507**Example**
508
509```ts
510import mdns from '@ohos.net.mdns'
511import { BusinessError } from '@ohos.base';
512
513// See mdns.createDiscoveryService.
514let context = getContext(this) as Context;
515let serviceType = "_print._tcp";
516let discoveryService = mdns.createDiscoveryService(context, serviceType);
517discoveryService.startSearchingMDNS();
518
519discoveryService.on('discoveryStart', (data: mdns.DiscoveryEventInfo) => {
520  console.log(JSON.stringify(data));
521});
522
523discoveryService.stopSearchingMDNS();
524
525discoveryService.off('discoveryStart', (data: mdns.DiscoveryEventInfo) => {
526  console.log(JSON.stringify(data));
527});
528```
529
530### on('discoveryStop')
531
532on(type: 'discoveryStop', callback: Callback\<DiscoveryEventInfo\>): void
533
534Enables listening for **discoveryStop** events.
535
536**System capability**: SystemCapability.Communication.NetManager.MDNS
537
538**Parameters**
539
540| Name       | Type                            | Mandatory| Description                                    |
541|-------------|--------------|-----------|-----------------------------------------------------|
542| type     | string                          | Yes      |Event type. This field has a fixed value of **discoveryStop**.<br>**discoveryStop**: event of stopping discovery of MDNS services on the LAN.|
543| callback | Callback\<DiscoveryEventInfo\>  | Yes      |Callback used to return the MDNS service and error information.     |
544
545**Example**
546
547```ts
548import mdns from '@ohos.net.mdns'
549import { BusinessError } from '@ohos.base';
550
551// See mdns.createDiscoveryService.
552let context = getContext(this) as Context;
553let serviceType = "_print._tcp";
554let discoveryService = mdns.createDiscoveryService(context, serviceType);
555discoveryService.startSearchingMDNS();
556
557discoveryService.on('discoveryStop', (data: mdns.DiscoveryEventInfo) => {
558  console.log(JSON.stringify(data));
559});
560
561discoveryService.stopSearchingMDNS();
562```
563
564### off('discoveryStop')
565
566off(type: 'discoveryStop', callback?: Callback\<DiscoveryEventInfo\>): void
567
568Disables listening for **discoveryStop** events.
569
570**System capability**: SystemCapability.Communication.NetManager.MDNS
571
572**Parameters**
573
574| Name       | Type                            | Mandatory| Description                                    |
575|-------------|--------------|-----------|-----------------------------------------------------|
576| type     | string                          | Yes      |Event type. This field has a fixed value of **discoveryStop**.<br>**discoveryStop**: event of stopping discovery of MDNS services on the LAN.|
577| callback | Callback\<DiscoveryEventInfo\>  | No      |Callback used to return the MDNS service and error information.     |
578
579**Example**
580
581```ts
582import mdns from '@ohos.net.mdns'
583import { BusinessError } from '@ohos.base';
584
585// See mdns.createDiscoveryService.
586let context = getContext(this) as Context;
587let serviceType = "_print._tcp";
588let discoveryService = mdns.createDiscoveryService(context, serviceType);
589discoveryService.startSearchingMDNS();
590
591discoveryService.on('discoveryStop', (data: mdns.DiscoveryEventInfo) => {
592  console.log(JSON.stringify(data));
593});
594
595discoveryService.stopSearchingMDNS();
596
597discoveryService.off('discoveryStop', (data: mdns.DiscoveryEventInfo) => {
598  console.log(JSON.stringify(data));
599});
600```
601
602### on('serviceFound')
603
604on(type: 'serviceFound', callback: Callback\<LocalServiceInfo>): void
605
606Enables listening for **serviceFound** events.
607
608**System capability**: SystemCapability.Communication.NetManager.MDNS
609
610**Parameters**
611
612| Name       | Type                            | Mandatory| Description                                    |
613|-------------|--------------|-----------|-----------------------------------------------------|
614| type     | string                          | Yes      |Event type. This field has a fixed value of **serviceFound**.<br>**serviceFound**: event indicating an MDNS service is found.|
615| callback | Callback<[LocalServiceInfo](#localserviceinfo)>                 | Yes       |   MDNS service information.     |
616
617**Example**
618
619```ts
620import mdns from '@ohos.net.mdns'
621import { BusinessError } from '@ohos.base';
622
623// See mdns.createDiscoveryService.
624let context = getContext(this) as Context;
625let serviceType = "_print._tcp";
626let discoveryService = mdns.createDiscoveryService(context, serviceType);
627discoveryService.startSearchingMDNS();
628
629discoveryService.on('serviceFound', (data: mdns.LocalServiceInfo) => {
630  console.log(JSON.stringify(data));
631});
632
633discoveryService.stopSearchingMDNS();
634```
635
636### off('serviceFound')
637
638off(type: 'serviceFound', callback?: Callback\<LocalServiceInfo>): void
639
640Disables listening for **serviceFound** events.
641
642**System capability**: SystemCapability.Communication.NetManager.MDNS
643
644**Parameters**
645
646| Name       | Type                            | Mandatory| Description                                    |
647|-------------|--------------|-----------|-----------------------------------------------------|
648| type     | string                          | Yes      |Event type. This field has a fixed value of **serviceFound**.<br>**serviceFound**: event indicating an MDNS service is found.|
649| callback | Callback<[LocalServiceInfo](#localserviceinfo)>                 | No       |   MDNS service information.     |
650
651**Example**
652
653```ts
654import mdns from '@ohos.net.mdns'
655import { BusinessError } from '@ohos.base';
656
657// See mdns.createDiscoveryService.
658let context = getContext(this) as Context;
659let serviceType = "_print._tcp";
660let discoveryService = mdns.createDiscoveryService(context, serviceType);
661discoveryService.startSearchingMDNS();
662
663discoveryService.on('serviceFound', (data: mdns.LocalServiceInfo) => {
664  console.log(JSON.stringify(data));
665});
666
667discoveryService.stopSearchingMDNS();
668
669discoveryService.off('serviceFound', (data: mdns.LocalServiceInfo) => {
670  console.log(JSON.stringify(data));
671});
672```
673
674### on('serviceLost')
675
676on(type: 'serviceLost', callback: Callback\<LocalServiceInfo>): void
677
678Enables listening for **serviceLost** events.
679
680**System capability**: SystemCapability.Communication.NetManager.MDNS
681
682**Parameters**
683
684| Name       | Type                            | Mandatory| Description                                    |
685|-------------|--------------|-----------|-----------------------------------------------------|
686| type     | string                          | Yes      |Event type. This field has a fixed value of **serviceLost**.<br>serviceLost: event indicating that an MDNS service is removed.|
687| callback | Callback<[LocalServiceInfo](#localserviceinfo)>   | Yes       |   MDNS service information.     |
688
689**Example**
690
691```ts
692import mdns from '@ohos.net.mdns'
693import { BusinessError } from '@ohos.base';
694
695// See mdns.createDiscoveryService.
696let context = getContext(this) as Context;
697let serviceType = "_print._tcp";
698let discoveryService = mdns.createDiscoveryService(context, serviceType);
699discoveryService.startSearchingMDNS();
700
701discoveryService.on('serviceLost', (data: mdns.LocalServiceInfo) => {
702  console.log(JSON.stringify(data));
703});
704
705discoveryService.stopSearchingMDNS();
706```
707
708### off('serviceLost')
709
710off(type: 'serviceLost', callback?: Callback\<LocalServiceInfo>): void
711
712Disables listening for **serviceLost** events.
713
714**System capability**: SystemCapability.Communication.NetManager.MDNS
715
716**Parameters**
717
718| Name       | Type                            | Mandatory| Description                                    |
719|-------------|--------------|-----------|-----------------------------------------------------|
720| type     | string                          | Yes      |Event type. This field has a fixed value of **serviceLost**.<br>serviceLost: event indicating that an MDNS service is removed.|
721| callback | Callback<[LocalServiceInfo](#localserviceinfo)>   | No       |   MDNS service information.     |
722
723**Example**
724
725```ts
726import mdns from '@ohos.net.mdns'
727import { BusinessError } from '@ohos.base';
728
729// See mdns.createDiscoveryService.
730let context = getContext(this) as Context;
731let serviceType = "_print._tcp";
732let discoveryService = mdns.createDiscoveryService(context, serviceType);
733discoveryService.startSearchingMDNS();
734
735discoveryService.on('serviceLost', (data: mdns.LocalServiceInfo) => {
736  console.log(JSON.stringify(data));
737});
738
739discoveryService.stopSearchingMDNS();
740
741discoveryService.off('serviceLost', (data: mdns.LocalServiceInfo) => {
742  console.log(JSON.stringify(data));
743});
744```
745
746## LocalServiceInfo
747
748Defines the MDNS service information.
749
750**System capability**: SystemCapability.Communication.NetManager.MDNS
751
752| Name                 | Type                               | Mandatory| Description                    |
753| --------------------- | ---------------------------------- | --- | ------------------------ |
754| serviceType   | string                             |  Yes|  Type of the MDNS service. The value is in the format of **\_\<name>.<_tcp/_udp>**, where **name** contains a maximum of 63 characters excluding periods (.). |
755| serviceName | string                             |  Yes|  Name of the MDNS service.  |
756| port            | number           |  No|  Port number of the MDNS server.          |
757| host           |  [NetAddress](js-apis-net-connection.md#netaddress) |  No|  IP address of the device that provides the MDNS service. The IP address is not effective when an MDNS service is added or removed.              |
758| serviceAttribute     | serviceAttribute\<[ServiceAttribute](#serviceattribute)> |  No|  MDNS service attribute information.              |
759
760## ServiceAttribute
761
762Defines the MDNS service attribute information.
763
764**System capability**: SystemCapability.Communication.NetManager.MDNS
765
766| Name                 | Type                               | Mandatory| Description                    |
767| --------------------- | ---------------------------------- | --- | ------------------------ |
768| key   | string                             |  Yes|  MDNS service attribute key. The value contains a maximum of 9 characters. |
769| value | Array\<number>                             |  Yes|  MDNS service attribute value.  |
770
771## DiscoveryEventInfo<sup>11+</sup>
772
773Defines the MDNS service event information.
774
775**System capability**: SystemCapability.Communication.NetManager.MDNS
776
777|    Name    |            Type                    | Mandatory| Description                 |
778| ----------- | ----------------------------------- | --- | --------------------- |
779| serviceInfo | LocalServiceInfo                    |  Yes|  MDNS service information.       |
780| errorCode   | MdnsError                           |  No|  Defines the MDNS error information.       |
781
782## MdnsError
783
784Defines the MDNS error information.
785
786**System capability**: SystemCapability.Communication.NetManager.MDNS
787
788| Name        | Value  | Description       |
789| --------------- | ---- | ----------- |
790| INTERNAL_ERROR  | 0    | Operation failed because of an internal error. (not supported currently) |
791| ALREADY_ACTIVE      | 1    | Operation failed because the service already exists. (not supported currently)|
792| MAX_LIMIT  | 2 | Operation failed because the number of requests exceeds the maximum value. (not supported currently)|
793