How to catch dart.io socket error?

251 views
Skip to first unread message

Niyazi Toros

unread,
Jul 5, 2018, 1:52:54 AM7/5/18
to mi...@dartlang.org
Updated: new question: how to send encrypted (3DES) data into aqueduct without getting any error?


Hi,

I am new to Dart, Aqueduct and socket. I am trying to learn all while I am working and making test project to understand. Any deep info and help appreciated.

Below code works for me as I expected in aqueduct. Problem is that I need to modify value before sending a socket, so I need to await like this. "await for (var data in socket)" Also I have some heavy work in 2 places. First I need to decrypt the request before sending to socket "socket.write('$tempData\r\n');".
Once I get a data from socket (listen) "await for (var data in socket)" I need to do some heavy work again before sending as response.

I had a 3 question that shown below.
Question-1: How to catch error if serevr is not reachable or down?
Question-2: How to catch error when trying send data to socket?
Question-3: How to catch error while socket listen returning data?


import 'package:aqueduct/aqueduct.dart';
import 'package:niyaziapi/niyaziapi.dart';
import 'package:niyaziapi/util/niyaziprivate.dart';

class LoginController extends Controller {
 
@override
 
Future<RequestOrResponse> processRequest(Request request) async {
 
if (request.path.variables.containsKey('value')) {
 
var _xPrivate = (request.path.variables['value']).trim();
 
var tempData = await getPrivate(_xPrivate);

 // TODO: Question-1: How to catch error if serevr is not reachable or down
 
var socket = await Socket.connect('192.168.1.22', 1024);
 
print("socket: $socket");

 // TODO: Question-2: How to catch error when trying send data to socket
 socket
.write('$tempData\r\n');

 // TODO: Question-3: How to catch error while socket listen (returning data)has error
 
await for (var data in socket) {
 
var reply = new String.fromCharCodes(data).trim();
 // Do some heavy work in here then
 
return new Response.ok("$reply");
 
}
 
} else {
 
return new Response.ok("wrong rquest");
 
}
 
}

 
Future<String> getPrivate(_xPrivate) async {
 // Do some heavy decryption in here then

 
return "data";
 
}
}


UPDATE:

when I testing I found that my code works. Only reason that I am sending 3DES data and has + and / character in it.

If you look at closely in first request, there is a + and / character in data which give me an error. /login/ziD7v0Ul99vmNWnxJRxZIiTY4zakNoq8GjM+oHROYz/YTHnd3NH1XfHRULY0jaHU 19ms 404 

on the other hand if I remove those character than I get perfect response.
/login/ziD7v0Ul99vmNWnxJRxZIiTY4zakNoq8GjMoHROYzYTHnd3NH1XfHRULY0jaHU 13 ms 200

So, question comes how to send encrypted (3DES) data into aqueduct without getting any error?







William Hesse

unread,
Jul 5, 2018, 4:29:09 AM7/5/18
to General Dart Discussion
You may get more replies if you post questions like these on StackOverflow.  For example, more than 100 dart questions were posted there this week.  You may also find questions already asked, that are similar to yours.


In general, when working with async functions and streams using the async/await keywords, error events are thrown as exceptions, and can be caught with try/catch.
If instead of using async/await, you use .then() on futures and .listen() on streams, then there are onError parameters and methods you would use to add an error handler.
The best way to answer basic questions like these is to find example code that does similar things to what you want to do, and see how it does it.

--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/c9495b92-3ccd-498f-95ac-80a9453196b1%40dartlang.org.


--
William Hesse

Niyazi Toros

unread,
Jul 5, 2018, 4:35:37 AM7/5/18
to mi...@dartlang.org
Thanks William, but most of the (currently 23 question on aqueduct) is from me. My question has some dart and some aqueduct request. I couldn't find (even I buy 2 online books) deep learning about dart.io and networking plus how to apply in to aqueduct. 

Niyazi Toros

unread,
Jul 5, 2018, 8:47:59 AM7/5/18
to Dart Misc
Going to Like Aqueduct twice :)

It was very simple: 
var _xPrivate = (request.path.variables['value']).trim(); change to:

var _xPrivate = request.path.remainingPath;
print("request: $_xPrivate");
Reply all
Reply to author
Forward
0 new messages