The Client class is an abstract base class used for network communication in the Teensy core. It inherits from Stream, meaning it can be used anywhere a Stream (like Serial) is expected. Because it is an abstract class, you do not instantiate Client directly; instead, you use a concrete implementation (such as a TCP client provided by a specific network hardware driver) that implements the following interface.
Connection Methods
connect(IPAddress ip, uint16_t port): Establishes a connection to a specific IP address and port. Returns an integer status code.connect(const char *host, uint16_t port): Establishes a connection to a hostname and port. Returns an integer status code.
Data Transmission (via Stream)
write(uint8_t): Writes a single byte.write(const uint8_t *buf, size_t size): Writes a buffer of bytes.available(): Returns the number of bytes available to read.read(): Reads a single byte.read(uint8_t *buf, size_t size): Reads a buffer of bytes.peek(): Returns the next available byte without removing it from the buffer.flush(): Ensures all outgoing data is transmitted.
Connection Management
stop(): Closes the connection.connected(): Returns 1 if the client is connected, 0 otherwise.operator bool(): Allows the client to be used in boolean contexts (e.g., if (client) { ... }) to check connection status.
// Example conceptual usage of a class implementing Client
// Note: Client itself is abstract and cannot be instantiated.
void loop() {
if (client.available() > 0) {
int data = client.read();
// Process data
}
if (client.connected()) {
client.write('A');
} else {
client.connect(IPAddress(192, 168, 1, 1), 80);
}
}