UDP服务器在接收数据报时崩溃
我发现了Dart语言。为了训练自己,我尝试编写一个简单的UDP服务器,记录收到的每个数据报 以下是我目前的代码:UDP服务器在接收数据报时崩溃,udp,dart,Udp,Dart,我发现了Dart语言。为了训练自己,我尝试编写一个简单的UDP服务器,记录收到的每个数据报 以下是我目前的代码: import 'dart:async'; import 'dart:convert'; import 'dart:io'; class UDPServer { static final UDPServer _instance = new UDPServer._internal(); // Socket used by the server. RawDatagramSo
import 'dart:async';
import 'dart:convert';
import 'dart:io';
class UDPServer {
static final UDPServer _instance = new UDPServer._internal();
// Socket used by the server.
RawDatagramSocket _udpSocket;
factory UDPServer() {
return _instance;
}
UDPServer._internal();
/// Starts the server.
Future start() async {
await RawDatagramSocket
.bind(InternetAddress.ANY_IP_V4, Protocol.udpPort, reuseAddress: true)
.then((RawDatagramSocket udpSocket) {
_udpSocket = udpSocket;
_udpSocket.listen((RawSocketEvent event) {
switch (event) {
case RawSocketEvent.READ:
_readDatagram();
break;
case RawSocketEvent.CLOSED:
print("Connection closed.");
break;
}
});
});
}
void _readDatagram() {
Datagram datagram = _udpSocket.receive();
if (datagram != null) {
String content = new String.fromCharCodes(datagram.data).trim();
String address = datagram.address.address;
print('Received "$content" from $address');
}
}
}
它在一定程度上工作得很好。。。在记录了一些消息之后,它就崩溃了。数字在1到5之间变化。IDEA只记录了与设备断开的连接。,仅此而已。我试图调试,但什么也没找到
有人知道它为什么会崩溃吗?非常感谢
编辑:我忘了提及,但我在一个颤振应用程序中使用了这段代码,它似乎就是从中产生的。有关更多信息,请参阅此部分。从命令行运行时,是否看到任何异常?@SethLadd它似乎与颤振相关。查看我的编辑