I would like to connect to a computer on the local network from a flutter app. Is there any way to get all machines connected to the local network? Like pinging broadcast address?
To discover your local network and list connected computers, printers and other network devices use ping_discover_network package.
Get local ip and discover network devices:
import 'package:wifi/wifi.dart';
import 'package:ping_discover_network/ping_discover_network.dart';
final String ip = await Wifi.ip;
final String subnet = ip.substring(0, ip.lastIndexOf('.'));
final int port = 80;
final stream = NetworkAnalyzer.discover(subnet, port);
stream.listen((NetworkAddress addr) {
if (addr.exists) {
print('Found device: ${addr.ip}');
}
});
Right now this library only works on Android, but...
https://pub.dartlang.org/packages/mdns
Setup mdns for whatever service you're running.
Discover the service as such
startMdnsDiscovery(String serviceType){
Mdns mdns = new Mdns(discoveryCallbacks: discoveryCallbacks);
mdns.startDiscovery(serviceType);
}
UPDATE Mar 26, 2020 - This no longer works with the newest versions of flutter, see below for an updated example.
Updated example: here
Also, what is MDNS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With