easy_udp_socket 1.2.1+3

  • Readme
  • Changelog
  • Installing
  • 78

Features #

  • receive Datagrams on demand without subscription.
  • receive timeout.
  • Various shorthand methods.

Usage #

usage example:

import 'dart:convert';
import 'package:easy_udp_socket/easy_udp_socket.dart';

start_server() async {
  // Create a EasyUDPSocket and bind to localhost:7777,
  // Note that you can also manually create a RawDatagramSocket 
  // and pass it to EasyUDPSocket(..) to create a EasyUDPSocket.
  final socket = await EasyUDPSocket.bindSimple(7777);

  while (true) {
    // Rather than subscribing to a stream of RawSocketEvents and
    // calling receive in the callback, which is the case when using
    // RawDatagramSocket, with EasyUDPSocket, you can get Datagram 
    // on demand with `receive` method.
    final datagram = await socket.receive();
    print('Server received: ${ascii.decode(datagram.data)} from ${datagram.port}');

    // use `sendBack` to send message to where a Datagram comes from.
    // This is a shorthand of:
    // socket.send(somedata, datagram.address, datagram.port);
    socket.sendBack(datagram, ascii.encode('pong'));
  }
}

start_client(int port) async {
  final socket = await EasyUDPSocket.bindSimple(port);
  socket.send(ascii.encode('ping'), 'localhost', 7777);
  final resp = await socket.receive();
  print('Client $port received: ${ascii.decode(resp.data)}');

  // `close` method of EasyUDPSocket is awaitable.
  await socket.close();
  print('Client $port closed');
}

start_multicast_server() async {
  //TODO: here create a multicast server
}

start_multicast_client(int port) async {
  final socket = await EasyUDPSocket.bindMulticast('224.0.2.200',port);
  if (socket != null) {
    socket.send(ascii.encode('ping'), '224.0.2.200', 7777);
    final resp = await socket.receive();
    print('Client $port received: ${ascii.decode(resp.data)}');

    // `close` method of EasyUDPSocket is awaitable.
    await socket.close();
    print('Client $port closed');
  }
}

start_broadcat_server(int port) async {
  //TODO: here create a broadcast server
}

// mark: the port must be server broadcast port
start_broadcast_client(int port) async {
  final socket = await EasyUDPSocket.bindBroadcast(port);
  if (socket != null) {
    socket.send(ascii.encode('ping'), '192.168.1.255', port);
    final resp = await socket.receive();
    print('Client $port received: ${ascii.decode(resp.data)}');

    // `close` method of EasyUDPSocket is awaitable.
    await socket.close();
    print('Client $port closed');
  }
}

main() async {
  start_server();
  await Future.delayed(Duration(seconds: 1));
  start_client(8001);
  start_client(8002);
  start_client(8003);
}

// Output:
//
// Server received: ping from 8001
// Server received: ping from 8002
// Server received: ping from 8003
// Client 8001 received: pong
// Client 8002 received: pong
// Client 8003 received: pong
// Client 8001 closed
// Client 8002 closed
// Client 8003 closed

with timeout:

...
final datagram = await socket.receive(timeout: 1000);
if(datagram == null) {
  print('Receive timeout');
  continue;
}
...

// Output:
//
// Server received: ping from 8001
// Server received: ping from 8002
// Server received: ping from 8003
// Client 8001 received: pong
// Client 8002 received: pong
// Client 8003 received: pong
// Client 8001 closed
// Client 8002 closed
// Client 8003 closed
// Receive timeout
// Receive timeout
// Receive timeout
// Receive timeout

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.2.1+3 #

  • add reusePort for ios

1.2.1+2 #

  • fix bug

1.2.1+1 #

  • modify readme

1.2.1 #

  • add try/catch handler
  • add unicast/multicast/broadcast creator

1.2.0 #

  • add explode option

1.1.0 #

  • add timeout feature

1.0.0 #

  • automatically do ns lookup in send

0.1.0+1 #

  • Update documentation

0.1.0 #

  • Initial version, created by Stagehand

Use this package as a library

1. Depend on it

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


dependencies:
  easy_udp_socket: ^1.2.1+3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or 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:easy_udp_socket/easy_udp_socket.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
55
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]
78
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Analysis suggestions

Package not compatible with runtime flutter-web on Web

Because:

  • package:easy_udp_socket/easy_udp_socket.dart that imports:
  • package:easy_udp_socket/src/easy_udp_socket_base.dart that imports:
  • dart:io

Package not compatible with runtime js

Because:

  • package:easy_udp_socket/easy_udp_socket.dart that imports:
  • package:easy_udp_socket/src/easy_udp_socket_base.dart that imports:
  • dart:io

Health suggestions

Format lib/easy_udp_socket.dart.

Run dartfmt to format lib/easy_udp_socket.dart.

Format lib/src/easy_udp_socket_base.dart.

Run dartfmt to format lib/src/easy_udp_socket_base.dart.

Maintenance suggestions

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and easy_udp_socket.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
async ^2.3.0 2.4.2
Transitive dependencies
collection 1.14.13 1.15.0-nullsafety
Dev dependencies
pedantic ^1.0.0
test ^1.0.0