Skip to main content

Actions

The ballerina/tcp package exposes the following clients:

ClientPurpose
ClientConnects 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​

FieldTypeDefaultDescription
localHoststring()Local interface address to bind the client socket to.
timeoutdecimal300Socket read timeout in seconds.
writeTimeoutdecimal300Socket write timeout in seconds.
secureSocketClientSecureSocket()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:

NameTypeRequiredDescription
databyte[]YesThe 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();