Flutter - The try catch block doesn't work on SocketException
I found the reason.
The RawDatagramSocket
I used to send data is the udp I bind to listen data.
When I create a new RawDatagramSocket
to send the data,the SocketException
was gone.
// error
_udpSocket =
await RawDatagramSocket.bind(InternetAddress.anyIPv4, _multicastPort, reuseAddress: true, reusePort: true);
_udpSocket.broadcastEnabled = true;
_udpSocket.listen((event) {
xxxxxx
})
_udpSocket.send()
/// works fine when create a new udp
_udpSocket =
await RawDatagramSocket.bind(InternetAddress.anyIPv4, _multicastPort, reuseAddress: true, reusePort: true);
_udpSocket.broadcastEnabled = true;
_udpSocket.listen((event) {})
final RawDatagramSocket udp = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 0);
udp.broadcastEnabled = true;
udp.send(utf8.encode(modelJson), _hotpotAddress, _multicastPort);
udp.close()