Actions
The ballerina/tcp package exposes the following clients:
| Client | Purpose |
|---|---|
Client | Connects to a remote TCP host to send and receive raw byte data. |
For event-driven integration, see the Trigger Reference.
Client
Connects to a remote TCP host to send and receive raw byte data.
Configuration
| Field | Type | Default | Description |
|---|---|---|---|
localHost | string | () | Local interface address to bind the client socket to. |
timeout | decimal | 300 | Socket read timeout in seconds. |
writeTimeout | decimal | 300 | Socket write timeout in seconds. |
secureSocket | ClientSecureSocket | () | SSL/TLS configuration for secure connections. |
Initializing the client
import ballerina/tcp;
tcp:Client tcpClient = check new ("localhost", 3000);
Operations
Data transfer
writeBytes
Sends byte array data to the connected remote host.
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
data | byte[] | Yes | The byte array to send to the remote host. |
Returns: error?
Sample code:
check tcpClient->writeBytes("Hello, server!".toBytes());
readBytes
Reads byte array data received from the remote host.
Returns: readonly & byte[]|error
Sample code:
readonly & byte[] response = check tcpClient->readBytes();
Sample response:
[72, 101, 108, 108, 111]
Connection management
close
Closes the TCP connection to the remote host.
Returns: error?
Sample code:
check tcpClient->close();