udp
udp copied to clipboard
mutiple requests not work
Im design an app that send a UDP broadcast. my code is simple send a fixed packet when i click in button. this is the function that is called. nut the problem is that the frist time work, the second show sent X bytes but not receive anything in others devices or response. i check with wireshart and the packet is not sent. sometimes can send 2/3 times but some only the first
searchDevices() async {
var sender = await UDP.bind(Endpoint.any(port: Port.any));
devices=[];
sender.socket?.broadcastEnabled=true;
// send a simple string to a broadcast endpoint on port 65001.
//sender.as
// creates a new UDP instance and binds it to the local address and the port
// 65002.
//var receiver = await UDP.bind(Endpoint.any(port: Port.any));
//receiver.socket?.broadcastEnabled=true;
// receiving\listening
sender.asStream(timeout: Duration(seconds: 5)).listen((datagram) {
var str = String.fromCharCodes(datagram!.data);
print(str);
devices.add(str);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(str)),
);
// receiver.close();
});
var dataLength = await sender.send( numbers, Endpoint.broadcast(port: Port(9998)));
print('$dataLength bytes sent.');
//receiver.socket.broadcastEnabled
// close the UDP instances and their sockets.
await Future.delayed(Duration(seconds: 5)); // Wait for responses.
// sender.close();
}