我的ServerSocket代码复制到下面,执行一个请求后,在下面抛出异常:
未处理的异常:SocketException:操作系统错误:连接被重置 对等方,errno = 54,地址= 127.0.0.1,端口= 9438
#0 _rootHandleUncaughtError。 (dart:async / zone.dart:1112:29)
#1 _microtaskLoop(dart:async / schedule_microtask.dart:41:21)
#2 _startMicrotaskLoop(dart:async / schedule_microtask.dart:50:5)
#3 _runPendingImmediateCallback(dart:isolate-patch / isolate_patch.dart:115:13)
#4 _RawReceivePortImpl._handleMessage(dart:isolate-patch / isolate_patch.dart:172:5)
我想原因是请求已关闭,并且异常无法捕获。
我该如何解决这个问题?
main() {
...
try {
ServerSocket ss = await ServerSocket.bind("127.0.0.1", 9438);
print('listening 127.0.0.1:9438');
ss.handleError((e) {});
await for (var req in ss) {
handleConnection(req);
}
} catch (e) {
print('server exception $e');
}
...
}
handleConnection(Socket req) async {
try {
Socket s = await Socket.connect('serverip', serverport);
req.map((v) => enc(v)).listen(s.add).onError((e) {});
s.map((vv) => enc(vv)).listen(req.add).onError((e) {});
await s.done;
// await req.done;
await s.close();
} on SocketException catch (e) {
print('e0=$e');
} catch (e) {
print('err=$e');
}
}