osociety / network_tools

Networking Tools library which can help you discover open ports, devices on subnet and many other things.
https://pub.dev/packages/network_tools
BSD 3-Clause "New" or "Revised" License
47 stars 13 forks source link

SocketException in example/lib/scan/mdns_scan.dart #192

Open xmlspyspring opened 8 months ago

xmlspyspring commented 8 months ago

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"
xmlspyspring commented 8 months ago

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,
    );
  }
xmlspyspring commented 8 months ago

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;
      });
    },);
git-elliot commented 8 months ago

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.

git-elliot commented 8 months ago

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.