Repository navigation
RawDatagramSocket.receive() throw unexpected error, when receive packet from local network. #206
Copy link
Copy link
Closed
Labels
engineflutter/engine related. See also e: labels.flutter/engine related. See also e: labels.platform-androidAndroid applications specificallyAndroid applications specifically
Milestone
Description
Issue by kyorohiro
Saturday Oct 17, 2015 at 03:38 GMT
Originally opened as https://github.com/flutter/engine/issues/1656
Following code throw error.
This code expect print "### hello!!"
- on mac
passed - on android
failed. throw error
[code]
import 'dart:io';
import 'dart:convert';
import 'dart:async';
main() async {
//
// 2015/10/16
// ANDROID: E/chromium: [ERROR:dart_error.cc(16)] Unhandled exception:ANDROID:
// E/chromium: SocketException: Receive failed (OS Error: Bad address, errno = 14),
// address = 0.0.0.0, port = 38081ANDROID: E/chromium: #0
// EchoServer.startServer.<startServer_async_body> (http://localhost:9888/lib/main.dart)
EchoServer echo = new EchoServer();
echo.startServer("0.0.0.0", 28081);
HelloClient hello = new HelloClient();
String te = await hello.sendHello("0.0.0.0", 28081);
print("### ${te}");
}
class HelloClient {
Future<String> sendHello(String address, int port) async {
List<int> buffer = [];
RawDatagramSocket socket = await RawDatagramSocket.bind("0.0.0.0", 0);
InternetAddress ad = new InternetAddress(address);
socket.send(UTF8.encode("hello!!"), ad, port);
await for (RawSocketEvent ev in socket.asBroadcastStream()) {
if (ev == RawSocketEvent.READ) {
Datagram dg = socket.receive();
if (dg != null) {
buffer.addAll(dg.data);
if (buffer.length >= 7) {
break;
}
}
}
}
socket.close();
return UTF8.decode(buffer);
}
}
class EchoServer {
RawDatagramSocket socket;
startServer(String host, int port) async {
socket = await RawDatagramSocket.bind(host, port);
await for (RawSocketEvent ev in socket.asBroadcastStream()) {
if (ev == RawSocketEvent.READ) {
try {
Datagram dg = socket.receive();
if (dg != null) {
print("${dg.address} ${dg.port} ${dg.data}");
socket.send(dg.data, dg.address, dg.port);
}
} catch (e) {
print("#########${e}");
}
}
}
}
bye() async {
socket.close();
}
}[ERROR]
[/Users/kiyohiro/hello_skyengine/dartio_udp] /bin/bash -l -c packages/flutter/sky_tool start
ANDROID: --------- beginning of main
ANDROID: --------- beginning of system
ANDROID: E/chromium: [ERROR:dart_error.cc(16)] Unhandled exception:
ANDROID: E/chromium: SocketException: Receive failed (OS Error: Bad address, errno = 14), address = 0.0.0.0, port = 28080
ANDROID: E/chromium: #0 EchoServer.startServer.<startServer_async_body> (http://localhost:9888/lib/main.dart)
ANDROID: E/chromium: #1 _asyncErrorWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:34)
[project]
https://github.com/kyorohiro/hello_skyengine/tree/master/dartio_udp
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
engineflutter/engine related. See also e: labels.flutter/engine related. See also e: labels.platform-androidAndroid applications specificallyAndroid applications specifically
Activity
eseidelGoogle commentedon Feb 10, 2017
I suspect that if this is still an error, it's down in dart:io. I've not yet confirmed that it is. FYI @zanderso
zanderso commentedon Feb 10, 2017
@fsc8000 marked //tests/standalone/io/raw_datagram_socket_test.dart as encountering a runtime error on Android. Florian, do you remember what the issue there was?
fsc8000 commentedon Feb 11, 2017
@zanderso The reason there is that raw_datagram_socket_test uses
Platform.executableand loads a different script, which does not work in the precompiled setting.eseidelGoogle commentedon Feb 11, 2017
This looks like it's opening a server port? Does Android let you do that?
In any case, this bug is more than a year old tons has changed in flutter in that time and we'll need to verify that the above code still fails first. :)
Hixie commentedon Mar 1, 2017
I just tried it and it seems to work now:
Initial commit of the Dart SDK autoroller for Flutter. (flutter#206)
github-actions commentedon Sep 5, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.