ping_discover_network 0.2.0+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 91

ping_discover_network #

Pub

Dart/Flutter library that allows to ping IP subnet and discover network devices.

Could be used to find printers (for example, on port 9100) and any other devices and services in local network.

The device should be connected to a Wi-Fi network. wifi package allows to get the local IP address / network subnet.

The library tested on both, Android and iOS platforms.

[pub.dev page] | [Documentation]

Getting Started #

Check complete examples in /example folder.

Discover available network devices in a given subnet on a given port:

import 'package:ping_discover_network/ping_discover_network.dart';

// NetworkAnalyzer.discover pings PORT:IP one by one according to timeout.
// NetworkAnalyzer.discover2 pings all PORT:IP addresses at once.

const port = 80;
final stream = NetworkAnalyzer.discover2(
  '192.168.0', port,
  timeout: Duration(milliseconds: 5000),
);

int found = 0;
stream.listen((NetworkAddress addr) {
  if (addr.exists) {
    found++;
    print('Found device: ${addr.ip}:$port');
  }
}).onDone(() => print('Finish. Found $found device(s)'));

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.discover2(subnet, port);
stream.listen((NetworkAddress addr) {
  if (addr.exists) {
    print('Found device: ${addr.ip}');
  }
});

Discover available network devices in a given subnet on a given port range:

import 'package:ping_discover_network/ping_discover_network.dart';

void checkPortRange(String subnet, int fromPort, int toPort) {
  if (fromPort > toPort) {
    return;
  }

  print('port ${fromPort}');

  final stream = NetworkAnalyzer.discover2(subnet, fromPort);

  stream.listen((NetworkAddress addr) {
    if (addr.exists) {
      print('Found device: ${addr.ip}:${fromPort}');
    }
  }).onDone(() {
    checkPortRange(subnet, fromPort + 1, toPort);
  });
}

checkPortRange('192.168.0', 400, 410);

[0.2.0]

  • Two pinging methods: ping ip addresses one by one OR all at once.

[0.1.3]

  • Ping ip addresses one by one

[0.1.2]

  • Added Flutter example: get the local ip and ping the subnet

[0.1.0] - [0.1.1]

  • Ping IP subnet on a given port
  • Added example: ping ports range
  • Added documentation

[0.0.1] - [0.0.2]

  • Initial release

example/main.dart

/*
 * ping_discover_network
 * Created by Andrey Ushakov
 * 
 * See LICENSE for distribution and usage details.
 */
import 'package:ping_discover_network/ping_discover_network.dart';

/// Discover available network devices in a given subnet on a given port
void main() async {
  // NetworkAnalyzer.discover pings PORT:IP one by one according to timeout.
  // NetworkAnalyzer.discover2 pings all PORT:IP addresses at once.

  const port = 80;
  final stream = NetworkAnalyzer.discover2(
    '192.168.0',
    port,
    timeout: Duration(milliseconds: 5000),
  );

  int found = 0;
  stream.listen((NetworkAddress addr) {
    // print('${addr.ip}:$port');
    if (addr.exists) {
      found++;
      print('Found device: ${addr.ip}:$port');
    }
  }).onDone(() => print('Finish. Found $found device(s)'));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ping_discover_network: ^0.2.0+1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ping_discover_network/ping_discover_network.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
81
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
91
Learn more about scoring.

We analyzed this package on Oct 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test