socketToServer static method

Future<SocketConnector> socketToServer({
  1. required InternetAddress addressA,
  2. required int portA,
  3. InternetAddress? addressB,
  4. int portB = 0,
  5. DataTransformer? transformAtoB,
  6. DataTransformer? transformBtoA,
  7. bool verbose = false,
  8. bool logTraffic = false,
  9. Duration timeout = SocketConnector.defaultTimeout,
  10. IOSink? logger,
})
  • Creates socket to portA on addressA

  • Binds to portB on addressB

  • Listens for a socket connection on portB port and joins it to the 'A' side

  • If portB is not provided then a port is chosen by the OS.

  • addressB defaults to InternetAddress.anyIPv4

Implementation

static Future<SocketConnector> socketToServer({
  required InternetAddress addressA,
  required int portA,

  /// Defaults to [InternetAddress.anyIPv4]
  InternetAddress? addressB,
  int portB = 0,
  DataTransformer? transformAtoB,
  DataTransformer? transformBtoA,
  bool verbose = false,
  bool logTraffic = false,
  Duration timeout = SocketConnector.defaultTimeout,
  IOSink? logger,
}) async {
  IOSink logSink = logger ?? stderr;
  addressB ??= InternetAddress.anyIPv4;

  SocketConnector connector = SocketConnector(
    verbose: verbose,
    logTraffic: logTraffic,
    timeout: timeout,
    logger: logSink,
  );

  // Create socket to an address and port
  Socket socket = await Socket.connect(addressA, portA);
  Side sideA = Side(socket, true, transformer: transformAtoB);
  unawaited(connector.handleSingleConnection(sideA).catchError((err) {
    logSink.writeln('ERROR $err from handleSingleConnection on sideA $sideA');
  }));

  // bind to side 'B' port
  connector._serverSocketB = await ServerSocket.bind(addressB, portB);

  // listen for connections to the 'B' side port
  connector._serverSocketB?.listen((socketB) {
    Side sideB = Side(socketB, false, transformer: transformBtoA);
    unawaited(connector.handleSingleConnection(sideB).catchError((err) {
      logSink
          .writeln('ERROR $err from handleSingleConnection on sideB $sideB');
    }));
  });
  return (connector);
}