Skip to content

RawDatagramSocket.receive() throw unexpected error, when receive packet from local network. #206

Closed

Description

@FlutterIssues

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

Activity

  1. modified the milestone: on Dec 16, 2015
  2. modified the milestones: 2: Make Early Adopters happy, on Jan 26, 2017
  3. eseidelGoogle commented on Feb 10, 2017

    @eseidelGoogle
    Contributor

    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

  4. zanderso commented on Feb 10, 2017

    @zanderso
    Member

    @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?

  5. fsc8000 commented on Feb 11, 2017

    @fsc8000

    @zanderso The reason there is that raw_datagram_socket_test uses Platform.executable and loads a different script, which does not work in the precompiled setting.

  6. eseidelGoogle commented on Feb 11, 2017

    @eseidelGoogle
    Contributor

    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. :)

  7. Hixie commented on Mar 1, 2017

    @Hixie
    Contributor

    I just tried it and it seems to work now:

    I/flutter (29146): InternetAddress('127.0.0.1', IP_V4) 44390 [104, 101, 108, 108, 111, 33, 33]
    I/flutter (29146): ### hello!!
    
  8. github-actions commented on Sep 5, 2021

    @github-actions

    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 -v and a minimal reproduction of the issue.

  9. locked as resolved and limited conversation to collaborators on Sep 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

engineflutter/engine related. See also e: labels.platform-androidAndroid applications specifically

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions