From 8032a5ca5a54be1d7bfe0f471464ded2a4375e3d Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 24 Sep 2020 20:44:56 +0200 Subject: [PATCH] Experiment with mdns to resolve a service --- lib/main_dns.dart | 37 +++++++++++++++++++++++++++++++++++++ pubspec.lock | 7 +++++++ pubspec.yaml | 1 + 3 files changed, 45 insertions(+) create mode 100644 lib/main_dns.dart diff --git a/lib/main_dns.dart b/lib/main_dns.dart new file mode 100644 index 00000000..4b168bce --- /dev/null +++ b/lib/main_dns.dart @@ -0,0 +1,37 @@ +import 'package:multicast_dns/multicast_dns.dart'; + +Future main() async { + // Parse the command line arguments. + + var name = '_dartobservatory._tcp.local'; + name = '_gitjournal._tcp'; + final MDnsClient client = MDnsClient(); + // Start the client with default options. + await client.start(); + + print("Client started"); + + //var query = ResourceRecordQuery.serverPointer(name); + var query = ResourceRecordQuery.serverPointer(name); + + // Get the PTR recod for the service. + await for (var ptr in client.lookup(query)) { + print("Got ptr $ptr"); + // 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( + ResourceRecordQuery.service(ptr.domainName))) { + // Domain name will be something like + // - "io.flutter.example@some-iphone.local._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.'); +} diff --git a/pubspec.lock b/pubspec.lock index cd39b9ae..96e40626 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -574,6 +574,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.9.6+3" + multicast_dns: + dependency: "direct main" + description: + name: multicast_dns + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.2" mutex: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 16229c63..e39c7198 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -56,6 +56,7 @@ dependencies: in_app_purchase: ^0.3.4+5 flutter_plugin_android_lifecycle: ^1.0.8 # for fixing the build timeago: ^2.0.27 + multicast_dns: ^0.2.2 dev_dependencies: flutter_launcher_icons: "^0.7.2"