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