Dart - Send an UDP broadcast

There are two problems:

  1. Use InternetAddress.anyIPv4 for binding on all network interfaces;

  2. Enable permission for broadcasting with property broadcastEnabled

Obviously use a broadcast address: for a /24 network use x.y.z.255 address.

This snippet works:

import 'dart:io';
import 'dart:convert';

main() {

  var DESTINATION_ADDRESS=InternetAddress("x.y.z.255");

  RawDatagramSocket.bind(InternetAddress.anyIPv4, 8889).then((RawDatagramSocket udpSocket) {
    udpSocket.broadcastEnabled = true;
    udpSocket.listen((e) {
      Datagram dg = udpSocket.receive();
      if (dg != null) {
        print("received ${dg.data}");
      }
    });
    List<int> data =utf8.encode('TEST');
    udpSocket.send(data, DESTINATION_ADDRESS, 8889);
  });
}