Search code examples
flutterdnsnetwork-programmingservice-discoverymdns

Flutter/Multicast DNS: How to test network discovery with MDNS


I have implemented network discovery with MDNS for my Flutter mobile app using the multicast_dns package.

The idea behind this was that the mobile app should automatically detect the server address on the local network. MDNS seems to be the more elegant approach instead of port scanning the entire local network.

The code looks something like this:

final MDnsClient client = MDnsClient(rawDatagramSocketFactory: factory);

const String name = '_http._tcp';
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))) {
      final String bundleId =
      ptr.domainName; //.substring(0, ptr.domainName.indexOf('@'));
      print('http server instance found at '
      '${srv.target}:${srv.port} for "$bundleId".');
  }
}
client.stop();

Now, I am searching for a way to mock said service with MDNS because the server isn't actually ready yet.

I have found something like this for this for MACOS:

dns-sd -R TestService _http._tcp . 3000

However, I found nothing like this for Windows (or Linux). How do I advertize/mock a http service per DNS in order to test my network discovery?


Solution

  • The easiest way is probably just follow these steps to get the same tool, dns-sd running on windows.

    Personally, I would just make a quick program in Go using the mdns package that I could compile to any OS I wanted.

    Either way works.