Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@FlutterIssues

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

Metadata

Metadata

Assignees

Labels

engineflutter/engine repository. 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