//SocketException: OS Error: Broken pipe, errno = 32, address = 0.0.0.0, port = 30100
//SocketException: OS Error: Connection reset by peer, errno = 104, address = 0.0.0.0, port = 30129
Cant catch these exceptions either try/catch blocks or handleError callbacks.
try {
ServerSocket.bind('0.0.0.0', port).then((serverSocket) {
serverSocket.handleError((error){
print('ERROR: ${error}');
});
serverSocket.listen((socket) {
socket.handleError((error){
print('ERROR: ${error}');
});
socket.transform(UTF8.decoder).listen((buffer) {
doSomething(socket, buffer)
});
});
});
} catch(exception, stackTrace) {
print(exception);
}
That might do it, i will check back with the results ASAP.
Yeah it keeps happening, i am probably doing something wrong
try {
await ServerSocket.bind('0.0.0.0', port).then((serverSocket) async {
serverSocket.handleError((error){
print('ERROR: ${error}');
});
await serverSocket.listen((socket) async {
socket.handleError((error){
print('ERROR: ${error}');
});
await socket.transform(UTF8.decoder).listen((buffer) {
parse(socket, buffer, db_name);
});
}, onError: (error) {
print('error: $error');
}, onDone: () {
print('socket closed.');
}, cancelOnError: true);
});
} catch(exception, stackTrace) {
print(exception);
}
If i managed to solve this i will go ahead and write a tutorial or something, i can't find anything about good practices for tcp servers in Dart.
Using the Socket interface in dart:io can be a bit tricky. One thing which is not documented particularly, is that the socket have a done getter which returns a Future that one must listen on, as errors can be reported on that as well as on the onError callback on listen.
socket.done.catchError((error) => print('error: $error'));
Also remember when using await you are already waiting for the Future and don't need to use .then as well. You should be able to replace:
await ServerSocket.bind('0.0.0.0', port).then((serverSocket) async {
...
}
with:
ServerSocket serverSocket = await ServerSocket.bind('0.0.0.0', port) {
...
}
When async errors happen information on the original place where the operation was scheduled is gone. You could take a look at http://news.dartlang.org/2016/01/unboxing-packages-stacktrace.html, to help get more information on async errors.
Thanks for the reply i fixed my future related code everywhere but i am getting this:
[warning] The getter 'done' is not defined for the class 'ServerSocket' (/srv/aries/lib/t.dart, line 403, col 18)
Edit: Just realized the done callback is for the created sockets. Will try it out.
Thanks again, managed to solve it. My main problem was that i did not understood futures as well as i thought i did.
@ghost, can you share solved code that you managed to use correct future? Thanks