TcpClient class

a simple client for arbitrary tcp connections.

This class will manage connecting and reconnecting to a server and will also manage sending and receiving data to that server

Constructors

TcpClient(String host, int port, {Uint8List? terminatorBytes, Duration timeout = const Duration(seconds: 2)})
terminatorBytes determines when a new item will be added to the data stream, for most TCP connections, the terminator is \r\n but some protocols might determine their own if this is set to null, data will be piped directly from the socket to the datastream as it comes in.
TcpClient.withStringTerminator(String host, int port, String terminatorString, {Duration timeout = const Duration(seconds: 2)})
Same as the default constructor, but builds the terminatorBytes from the UTF8 encoding of the given string.

Properties

connectionStream Stream<TcpConnectionState>
a stream to give visibility to the status of this connection which is helpful whenever the stream is in a connecting or reconnecting state
no setter
connectionType TcpConnectionType
getter/setter pair
dataStream Stream<Uint8List>
the stream that passes data back to listeners
no setter
hashCode int
The hash code for this object.
no setterinherited
host String
final
isConnected bool
no setter
isPersistent bool
no setter
port int
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
shouldReconnect bool
no setter
status TcpConnectionState
getter/setter pair
stringStream Stream<String>
transform the data stream into a stream of strings... invalid characters will be decoded to the unicode replacement character U+FFFD (�)
no setter
terminatorBytes Uint8List?
for persistent connections, these bytes serve as the "terminator" of a tcp response if terminator is null, all the data will be streamed as it is received.
getter/setter pair
timeout Duration
getter/setter pair

Methods

add(Uint8List bytes) → void
will pass the given bytes directly to the Socket.add method Note: you should probably use send instead.
close() Future
will wait until all socket data has been flushed to the socket and then will close it.
dbg(Object msg) → void
flush() → void
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
send(Object msg, {dynamic appendTerminator = true}) → void
will send a message to the socket. If the message is a list of bytes, (i.e. Uint8List or List<int>) the bytes will be sent directly.
sendAndClose(Object msg, {dynamic appendTerminator = true}) Future
will send a single message to the socket and then immediately close it. Use this when you don't care about the response.
sendAndWait(Object msg, {dynamic appendTerminator = true}) Future<TcpMessage?>
will send a message to the socket, and will reply with a future that resolves to the next message received on the socket.
stopTrying() → void
toString() String
A string representation of this object.
inherited
write(Object msg) → void
will pass the given object directly to the Socket.write method (using Object.toString if needed) Note: you should probably use send instead.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

debug bool
getter/setter pair

Static Methods

connect(String host, int port, {String? terminatorString, List<int>? terminatorBytes, TcpConnectionType connectionType = TcpConnectionType.untilClosed, Duration timeout = const Duration(seconds: 2)}) Future<TcpClient>
creates a TcpClient and immediately attempts to connect. If successful or if the requested connection should manage reconnecting on it's own, the client will complete the future. Otherwise, an exception will be thrown.
connectPersistent(String host, int port, {String? terminatorString, List<int>? terminatorBytes, Duration timeout = const Duration(seconds: 2)}) Future<TcpClient>
creates an auto-reconnecting TcpClient and immediately attempts to connect. after the connection has been initiated, the future will be completed.