main.go
```go
package main
import (
"fmt"
"github.com/hashicorp/mdns"
"os"
"net/http"
)
func health(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
}
func main() {
hostname, err := os.Hostname()
if err != nil {
panic(fmt.Sprintf("Error getting current hostname, description: %s", err.Error()))
}
info := []string{"mDNS get server"}
service, err := mdns.NewMDNSService(hostname, "_test._tcp", "", "", 8080, nil, info)
if err != nil {
panic(fmt.Sprintf("Error while exporting the service, description: %s", err.Error()))
}
server, err := mdns.NewServer(&mdns.Config{Zone: service})
if err != nil {
panic(fmt.Sprintf("Error while setting the discover server up, description: %s", err.Error()))
}
defer server.Shutdown()
http.HandleFunc("/", health)
http.ListenAndServe(":8081",nil)
}
```
Considering the following client (which I got from [here](https://pub.dev/packages/multicast_dns/example)):
multicast_dns.start() function
```dart
Future start({
InternetAddress? listenAddress,
NetworkInterfacesFactory? interfacesFactory,
int mDnsPort = mDnsPort,
InternetAddress? mDnsAddress,
}) async {
listenAddress ??= InternetAddress.anyIPv4;
interfacesFactory ??= allInterfacesFactory;
assert(listenAddress.address == InternetAddress.anyIPv4.address ||
listenAddress.address == InternetAddress.anyIPv6.address);
if (_started || _starting) {
return;
}
_starting = true;
final int selectedMDnsPort = _mDnsPort = mDnsPort;
_mDnsAddress = mDnsAddress;
// Listen on all addresses.
final RawDatagramSocket incoming = await _rawDatagramSocketFactory(
listenAddress.address,
selectedMDnsPort,
reuseAddress: true,
reusePort: true,
ttl: 255,
);
_mDnsAddress ??= incoming.address.type == InternetAddressType.IPv4
? mDnsAddressIPv4
: mDnsAddressIPv6;
final List interfaces =
(await interfacesFactory(listenAddress.type)).toList();
for (final NetworkInterface interface in interfaces) {
// Create a socket for sending on each adapter.
final InternetAddress targetAddress = interface.addresses[0];
// Join multicast on this interface.
incoming.joinMulticast(_mDnsAddress!, interface);
}
// Can't send to IPv6 any address.
if (incoming.address != InternetAddress.anyIPv6) {
_sockets.add(incoming);
} else {
_toBeClosed.add(incoming);
}
incoming.listen((RawSocketEvent event) => _handleIncoming(event, incoming));
_started = true;
_starting = false;
}
```
The idea is indeed to let the first 3 `IP`s to send the `QM` packets which response should be _hopefully_ captured by the `incoming` socket before the socket on `0.0.0.0` would send the `QM` packet too.
# Wireshark filter
```shell
(ip.src==192.168.2.7 || ip.src==192.168.2.16) && udp.port eq 5353
```
# Related Issue
- It should resolves issue [#79772](https://github.com/flutter/flutter/issues/79772)
# Disclaimers
- I'm not expert in `flutter`/`dart`, I pulled the code and I tried to debug it with help of uncle `google` and `print()`;
- I don't have a huge expertise in networking but I _know_ how to play a bit with `Wireshark`, inspect the networks and craft packets.
---
packages/multicast_dns/CHANGELOG.md | 3 +-
packages/multicast_dns/README.md | 2 +
packages/multicast_dns/lib/multicast_dns.dart | 57 ++++++-----
packages/multicast_dns/pubspec.yaml | 2 +-
packages/multicast_dns/test/client_test.dart | 98 +++++++++++++++++++
5 files changed, 131 insertions(+), 31 deletions(-)
diff --git a/packages/multicast_dns/CHANGELOG.md b/packages/multicast_dns/CHANGELOG.md
index 9cb48301ba2f6..6db175a0db9fa 100644
--- a/packages/multicast_dns/CHANGELOG.md
+++ b/packages/multicast_dns/CHANGELOG.md
@@ -1,5 +1,6 @@
-## NEXT
+## 0.3.2+7
+* Optimized Socket Binding: Always bind to 0.0.0.0 for simplicity and efficiency.
* Updates minimum supported SDK version to Flutter 3.16/Dart 3.2.
## 0.3.2+6
diff --git a/packages/multicast_dns/README.md b/packages/multicast_dns/README.md
index 10cd33e4bb636..c161b6fd98d42 100644
--- a/packages/multicast_dns/README.md
+++ b/packages/multicast_dns/README.md
@@ -1,5 +1,7 @@
# Multicast DNS package
+Based on [RFC 6762 Multicast DNS](https://datatracker.ietf.org/doc/html/rfc6762).
+
[![pub package](https://img.shields.io/pub/v/multicast_dns.svg)](
https://pub.dartlang.org/packages/multicast_dns)
diff --git a/packages/multicast_dns/lib/multicast_dns.dart b/packages/multicast_dns/lib/multicast_dns.dart
index 336ac427930e8..d6e2e6b06c0b9 100644
--- a/packages/multicast_dns/lib/multicast_dns.dart
+++ b/packages/multicast_dns/lib/multicast_dns.dart
@@ -48,8 +48,8 @@ class MDnsClient {
bool _starting = false;
bool _started = false;
- final List