/tcp_socket_flutter

Primary LanguageDartMIT LicenseMIT

TCP Socket Flutter

Introduction

This is a Flutter package that allows you to connect to a socket over the net. All data is then read using UTF-8.

Setup

Config

  TCPSocketSetUp.setConfig(
    const SocketConfig(
      port: 8000,
      numberSplit: 10000,
      timeoutEachTimesSendData: Duration(milliseconds: 50),
    ),
  );

Device Info

await TCPSocketSetUp.init();

Server

  final TCPSocketServer _server = TCPSocketServer();
  final result = await _server.initServer(
    onData: (ip, sourcePort, event) {
        print('Server receive data from: $ip:$sourcePort');
        print('Server receive data: $event');
    },
    onDone: (ip, sourcePort) {},
    onError: (error, ip, sourcePort) {},
  );
  print(result);

Server send data

  await _server.sendData(
    FormDataSending(
      type: 'Server send info',
      data: getRandomString(1000000),
    ),
  );

Client

  final TCPSocketClient _client = TCPSocketClient();
  final result = await _client.connectToServer(
    '192.168.0.101',
    onData: (event) {
      print('Client receive data: $event');
    },
    onDone: () {},
    onError: (error) {},
  );
  print('Connect to server $result');

Client send data

  await _client.sendData(
    FormDataSending(
      type: 'Client send info',
      data: getRandomString(1000000),
    ),
  );

License and contributors