utp 1.0.1 copy "utp: ^1.0.1" to clipboard
utp: ^1.0.1 copied to clipboard

uTorrent Transport Protocol (BEP0029 specification) library implemented by pure Dart.

example/utp_example.dart

import 'dart:convert';
import 'dart:io';

import 'package:utp/src/utp_protocol_implement.dart';

void main() async {
  var total = 21889;
  var chinese = '中文字符甲乙丙丁戊己庚辛\n';
  int startTime;
  var ss = await ServerUTPSocket.bind(InternetAddress.anyIPv4, 0);
  var port = ss.port;
  print('[Server] start to listening at $port');
  var receive = <int>[];
  ss.listen((socket) {
    startTime = DateTime.now().microsecondsSinceEpoch;
    print(
        '[Server] ${socket.remoteAddress.address}:${socket.remotePort}[${socket.connectionId}] connected');
    socket.listen((data) {
      receive.addAll(data);
    }, onDone: () async {
      var endTime = DateTime.now().microsecondsSinceEpoch;
      var spendTime = (endTime - startTime) / 1000000;
      var result = utf8.decode(receive);
      var totalSize = receive.length / 1024;
      receive.clear();
      print('$result');
      await ss.close();
      print(
          '[Server] Remote ${socket.remoteAddress.address}:${socket.remotePort}[${socket.connectionId}] closed,so close server too');
      print(
          '[Server] Server closed. Receive $totalSize kb datas , speed ${(totalSize / spendTime).toStringAsFixed(3)} kb/s');
    });
  });

  var pool = UTPSocketClient();
  var s1 = await pool.connect(InternetAddress.tryParse('127.0.0.1'), port);
  var cbys = utf8.encode(chinese).length;
  print(
      '[Client] Connect ${s1.remoteAddress.address}:${s1.remotePort}[${s1.connectionId}] successfully. Start to send ${(total * cbys) / 1024}kb datas');
  for (var i = 0; i < total; i++) {
    s1.write(chinese);
  }
  await s1.close();
  print('[Client] Client closed');
  await pool.close();
}
2
likes
30
pub points
34%
popularity

Publisher

unverified uploader

uTorrent Transport Protocol (BEP0029 specification) library implemented by pure Dart.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (LICENSE)

More

Packages that depend on utp