mdns_responder 0.0.2
mdns_responder: ^0.0.2 copied to clipboard
A Flutter plugin for publishing mDNS (Multicast DNS) services on Android and iOS
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:mdns_responder/mdns_responder.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
MdnsResponder mdnsResponderPlugin = MdnsResponder();
@override
void initState() {
super.initState();
_startMdnsResponder();
}
Future<void> _startMdnsResponder() async {
final service = MdnsService(
name: 'peer-abc123',
type: '_p2p._udp',
port: 4001,
hostAddresses: ['192.168.1.42', '10.0.0.120', '2001:db8:85a3::8a2e:370:7334'],
attributes: {'id': 'QmPeerID', 'proto': 'libp2p'},
subtypes: ['_libp2p'],
);
mdnsResponderPlugin
.publishService(service)
.then((_) {
if (kDebugMode) {
print('Service published: ${service.name}');
}
})
.catchError((error) {
if (kDebugMode) {
print('Failed to publish service: $error');
}
});
await Future.delayed(const Duration(seconds: 5));
mdnsResponderPlugin
.stopService()
.then((_) {
if (kDebugMode) {
print('Service stopped');
}
})
.catchError((error) {
if (kDebugMode) {
print('Failed to stop service: $error');
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('mDNS Server Responder example App')),
body: Center(child: Text('Check Wireshark / other PCAP tool for mDNS packets')),
),
);
}
}