Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How to Implement Network Service Discovery

Tags:

flutter

dart

I want to discover the deices which are running a specified service exposed to the local network, but I don't know how to. For example I want local IP address and port number of the devices running service '_googlecast._tcp.'.

Is there any way to achieve this in Flutter ?

Thanks in advance

like image 288
Yogesh Chawla Avatar asked Oct 16 '25 04:10

Yogesh Chawla


1 Answers

Check multicast DNS package: A Dart package to do service discovery over multicast DNS (mDNS), Bonjour, and Avahi. Essentially, create a mDNS Client and get PTR record for the service, as:

const String name = '_googlecast._tcp.local';
  final MDnsClient client = MDnsClient();
  await client.start();
  // Get the PTR recod for the service.
  await for (PtrResourceRecord ptr in client
      .lookup<PtrResourceRecord>(ResourceRecordQuery.serverPointer(name))) {
    // Use the domainName from the PTR record to get the SRV record,
    // which will have the port and local hostname.
    // Note that duplicate messages may come through, especially if any
    // other mDNS queries are running elsewhere on the machine.
    await for (SrvResourceRecord srv in client.lookup<SrvResourceRecord>(
        ResourceRecordQuery.service(ptr.domainName))) {
      // Domain name will be something like "[email protected]._dartobservatory._tcp.local"
      final String bundleId =
          ptr.domainName; //.substring(0, ptr.domainName.indexOf('@'));
      print('Dart observatory instance found at '
          '${srv.target}:${srv.port} for "$bundleId".');
    }
  }
  client.stop();

  print('Done.');
like image 102
Ashwani Kumar Avatar answered Oct 17 '25 17:10

Ashwani Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!