SocketException in example/lib/scan/mdns_scan.dart
I ran example/lib/scan/mdns_scan.dart and got the following error:
C:/a_dev/flutter-sdk/bin/cache/dart-sdk/bin/dart.exe --enable-asserts C:\Users\xmlspyspring\IdeaProjects\network_tools\example\lib\scan\mdns_scan.dart
Unhandled exception:
SocketException: Failed to create datagram socket (OS Error: 在其上下文中,该请求的地址无效。---> In its context, the requested address is invalid
, errno = 10049), address = , port = 5353
#0 _NativeSocket.bindDatagram (dart:io-patch/socket_patch.dart:1051:7)
<asynchronous suspension>
#1 _RawDatagramSocket.bind.<anonymous closure> (dart:io-patch/socket_patch.dart:2558:15)
<asynchronous suspension>
#2 MDnsClient.start (package:multicast_dns/multicast_dns.dart:135:40)
<asynchronous suspension>
#3 MdnsScannerServiceImpl.findingMdnsWithAddress (package:network_tools/src/services/impls/mdns_scanner_service_impl.dart:73:5)
<asynchronous suspension>
Process finished with exit code 255
My Environment: windows 10 chinese
PS C:\Users\xmlspyspring\IdeaProjects\network_tools> dart --version
Dart SDK version: 3.3.1 (stable) (Wed Mar 6 13:09:19 2024 +0000) on "windows_x64"
I know what the reason is, but I can't change it. This is caused by a bug in the multicast_dns.
The file in this library multicast_dns - 0.3.2 + 6\ lib\ multicast_dns.dart line 63 gets all NetInterfaces, but I have two NetworkInterfaces that are closed, so using a socket to connect the two NetInterfaces makes an error.
Future<Iterable<NetworkInterface>> allInterfacesFactory(
InternetAddressType type) {
return NetworkInterface.list( // ---> here
includeLinkLocal: true,
type: type,
includeLoopback: true,
);
}
I found a solution, but I don't know if it's right.
in file lib/src/services/impls/mdns_scanner_service_impl.dart line 73 change
await client.start();
to
await client.start(interfacesFactory: (InternetAddressType type){
return NetworkInterface.list(
includeLinkLocal: false, // old is true
type: type,
includeLoopback: true,
);
},);
or
await client.start(interfacesFactory: (InternetAddressType type){
return NetworkInterface.list(
includeLinkLocal: true,
type: type,
includeLoopback: true,
).then((List<NetworkInterface> list){
list.removeWhere((item){
if(ping item is not ok) return true; // remove this item
return false; // ping item is ok
});
return list;
});
},);
includeLinkLocal: false will exclude interfaces for everyone. You have 2 interfaces closed but others can have open. If we can determine if a network interface is not closed then we fix this. Probably you can to clone this repo and raise a PR for the fix.
But I don't think that this is the right place to fix it. If we can fix it in package multicast_dns then it would be better. Can you open the same issue in multicast_dns also and let's see if they can fix it.