UDP multicasting working properly in Dart(Linux) but not in Flutter(Android Emulator)

Viewed 740

I am trying to make a service discovery for my app, So I tried it first in Dart and it worked. When I built it on flutter, it doesnt work properly. I have acquired the wifimulticastlock through platform channels in flutter and also have given permissions for it in the android manifest.

The discoverer in the app doesnt recieve or detect the UDP packets sent to the multicast group by the advertiser(on both dart and app). But when i do the same on dart, it detects the UDP packets sent from the advertiser. The advertiser works on both the app and dart.

This is the service advertiser:

import 'dart:async';
import 'dart:io'
    show RawDatagramSocket, RawSocketEvent, InternetAddress, Datagram;
import 'dart:convert' show utf8;
import 'dart:async' show Timer;

main() {
  RawDatagramSocket dsocket;
  RawDatagramSocket.bind(InternetAddress.anyIPv4, 0).then((datagramSocket) {
    dsocket = datagramSocket;
    datagramSocket.broadcastEnabled = true;
    datagramSocket.readEventsEnabled = true;
    datagramSocket.listen((RawSocketEvent event) {
      if (event == RawSocketEvent.read) {
        Datagram datagram = datagramSocket.receive();
        if (datagram != null) {
          print('${datagram.address}:${datagram.port} -- ${utf8.decode(datagram.data)}');
          //datagramSocket.close();
        }
      }
    });
  });
  Timer.periodic(Duration(seconds: 1), (timer) {
    if(timer.isActive){
      dsocket.send("io.app.service".codeUnits, InternetAddress("224.0.0.1"), 8000);
    }
  });
}

This is my service discoverer:

import 'dart:io'
    show RawDatagramSocket, RawSocketEvent, InternetAddress, Datagram;
import 'dart:convert' show utf8;

main() =>
RawDatagramSocket.bind(InternetAddress.anyIPv4, 8000)
    .then((datagramSocket) {
  datagramSocket.readEventsEnabled = true;
  datagramSocket.joinMulticast(InternetAddress("224.0.0.1"));
  datagramSocket.listen((RawSocketEvent event) {
    if (event == RawSocketEvent.read) {
      Datagram datagram = datagramSocket.receive();
      if ((datagram != null)&&(utf8.decode(datagram.data)=="io.app.service")) {
        datagramSocket.send(datagram.data, datagram.address, datagram.port);
        print('${datagram.address}:${datagram.port} -- ${utf8.decode(datagram.data)}');
      }
    }
  });
});

This works when i run both in dart. This also works when I run the advertiser on Flutter and the discoveror on Dart. This doesnt work any other way.

Also the discoveror on dart doesnt detect the advertisers from external android devices in the same LAN.

Any help would be much appreciated.

0 Answers
Related