i have created a LAN using hotspot option of an android device and made other android devices conneted to this LAN. i need to send data from the hotspot device to all other connected devices ?. until now i can send data from one connected device to another, but not to the hotspot device.
i tried to do that using rawDatagramSocket class in flutter. because the app should hybrid.
i don't know if the device that created the LAN have the possibility to send and receive from other connected devices, or it's just impossible?
for the code:
- the send function:
String _data= "test";
void _sendData() async {
var DESTINATIONADDRESS = InternetAddress("255.255.255.255");
RawDatagramSocket.bind(InternetAddress.anyIPv4, 9000).then((RawDatagramSocket udpSocket) {
udpSocket.broadcastEnabled = true;
udpSocket.listen((e) {
Datagram? dg = udpSocket.receive();
if (dg != null) {
}
});
List<int> data = utf8.encode(_data);
udpSocket.send(data, DESTINATIONADDRESS, 8889);
});
}
- the receive function:
String _data= "";
void _receiveData() async {
RawDatagramSocket.bind(InternetAddress.anyIPv4, 8889).then((RawDatagramSocket udpSocket) {
udpSocket.broadcastEnabled = true;
udpSocket.listen((e) {
Datagram? dg = udpSocket.receive();
if (dg != null) {
String data = utf8.decode(dg.data);
setState(() {
_data = "received : $data";
});
}
});
});
}
if someone looks for the answer, i was just changed the broadcast address from 255.255.255.255 to the broadcast of my network address (x.y.z.255) in my case 192.168.43.255 and it worked.