socketToServer static method
Future<SocketConnector>
socketToServer({
- required InternetAddress addressA,
- required int portA,
- InternetAddress? addressB,
- int portB = 0,
- DataTransformer? transformAtoB,
- DataTransformer? transformBtoA,
- bool verbose = false,
- bool logTraffic = false,
- Duration timeout = SocketConnector.defaultTimeout,
- IOSink? logger,
-
Creates socket to
portA
onaddressA
-
Binds to
portB
onaddressB
-
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);
}