Memo : Flutter x Dart [2015-2016]
  • Introduction
  • hello
    • helloworld
    • build flutter
    • build as standalone android app(old)
      • draw text
      • draw image
      • set icon
    • build as standalone apk(now)
    • build as ios app
    • build as linux app
    • build as mac app
    • build mojo
    • build as chromeapp
  • rendering
    • draw_rect
    • anime_rect
    • anime_rect(use animation.dart)
    • draw_image
    • rotate_image
    • draw_image_from_assets
    • sound_test(rendering & service)
    • touch_test
    • multitouch_test
    • [@]demo::mini game(spacewar!)
    • draw_text
    • draw_path
    • draw_vertexs
    • draw_vertexs_with_image
    • [@]demo:3d anime
    • input text from ime(RenderObject)
  • widget
    • text
    • network image
    • assets image
    • raw image
    • container
    • row
    • col
    • flexible
    • navigator
    • stateful component
    • scaffold
    • scaffold x drawer
    • scrollable viewport
    • scrollable list
    • input text from ime(EditableText)
  • dart:io
    • HttpClient Post & Get
    • NetworkInterface
    • TCP Socket
    • UDP Socket
    • File Path(dart:io & service)
    • File Create & Permission
  • dart:ui
    • draw rect
    • pointer event
  • mojo for flutter
    • get and post request
    • sensor test
    • [@]demo:gravity sensor
  • 2d physics for scratch
    • collision without rotation
    • [@]democollision without friction
    • [b]
  • 2d physics for newton
  • mojo for native
    • build mojo
  • 48 hour trial
    • create iron heart
    • tiny tetris
Powered by GitBook
On this page

Was this helpful?

  1. dart:io

UDP Socket

PreviousTCP SocketNextFile Path(dart:io & service)

Last updated 5 years ago

Was this helpful?

  • [error]

// following code is checked in 2016/01/13
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();
  }
}
https://github.com/kyorohiro/hello_skyengine/tree/master/hello
https://github.com/flutter/engine/issues/1656