Module tcp
API

ballerina/tcp Ballerina library
Clients
tcp: Caller
Represents caller object in tcp service remote methods.
writeBytes
function writeBytes(byte[] data) returns Error?
Sends the response as byte[] to the same remote host.
Parameters
- data byte[] - The data need to be sent to the remote host
Return Type
- Error? -
()
or else atcp:Error
if the given data cannot be sent
close
function close() returns Error?
Close the remote connection.
Return Type
- Error? -
()
or else atcp:Error
if the connection cannot be properly closed
tcp: Client
Initializes the TCP connection client based on the provided configurations.
Constructor
Initializes the TCP client based on the provided configurations.
tcp:Client|tcp:Error? socketClient = new("www.remote.com", 80,
localHost = "localHost");
init (string remoteHost, int remotePort, *ClientConfiguration config)
- remoteHost string - The hostname or the IP address of the remote host
- remotePort int - The port number of the remote host
- config *ClientConfiguration - Connection-oriented client-related configurations
writeBytes
function writeBytes(byte[] data) returns Error?
Sends the given data to the connected remote host.
tcp:Error? result = socketClient->writeBytes("msg".toBytes());
Parameters
- data byte[] - The data that need to be sent to the connected remote host
Return Type
- Error? -
()
or else atcp:Error
if the given data cannot be sent
readBytes
function readBytes() returns (readonly & byte[])|Error
Reads data only from the connected remote host.
(readonly & byte[])|tcp:Error result = socketClient->readBytes();
Return Type
- (readonly & byte[])|Error - The
readonly & byte[]
or else atcp:Error
if the data cannot be read from the remote host
close
function close() returns Error?
Frees up the occupied socket.
tcp:Error? closeResult = socketClient->close();
Return Type
- Error? - A
tcp:Error
if it cannot close the connection or else()
Service types
tcp: Service
Represent TCP Listener service type.
tcp: ConnectionService
Represent TCP Listener ConnectionService service type.
Enums
tcp: Protocol
Represents protocol options.
Members
Listeners
tcp: Listener
This is used for creating TCP server endpoints. A TCP server endpoint is
capable of responding to remote callers. The Listener
is responsible for
initializing the endpoint using the provided configurations.
Constructor
Initializes the TCP listener based on the provided configurations.
listener Listener|error? server1 = new (8080);
init (int localPort, *ListenerConfiguration config)
- localPort int - The port number of the remote service
- config *ListenerConfiguration - Configurations related to the
tcp:Listener
attach
Binds a service to the tcp:Listener
.
tcp:error? result = tcpListener.attach(helloService);
Parameters
- tcpService Service - Type descriptor of the service
Return Type
- error? -
()
or else atcp:Error
upon failure to register the listener
'start
function 'start() returns error?
Starts the registered service programmatically.
Return Type
- error? - An
error
if an error occurred during the listener starting process
gracefulStop
function gracefulStop() returns error?
Stops the service listener gracefully. Already-accepted requests will be served before the connection closure.
Return Type
- error? - An
error
if an error occurred during the listener stopping process
immediateStop
function immediateStop() returns error?
Stops the service listener immediately. It is not implemented yet.
Return Type
- error? - An
error
if an error occurred during the listener stop process
detach
Stops consuming messages and detaches the service from the tcp:Listener
.
tcp:error? result = tcpListener.detach(helloService);
Parameters
- tcpService Service - Type descriptor of the service
Return Type
- error? -
()
or else atcp:Error
upon failure to detach the service
Records
tcp: CertKey
Represents a combination of the certificate, private key, and private key password (if encrypted).
Fields
- certFile string - A file containing the certificate
- keyFile string - A file containing the private key in PKCS8 format
- keyPassword? string - Password of the private key if it is encrypted
tcp: ClientConfiguration
Configurations for the connection-oriented TCP client.
Fields
- localHost? string - Local binding of the interface
- timeout decimal(default 300) - The socket reading timeout value to be used in seconds. If this is not set, the default value of 300 seconds(5 minutes) will be used
- writeTimeout decimal(default 300) - The socket write timeout value to be used in seconds. If this is not set, the default value of 300 seconds(5 minutes) will be used
- secureSocket? ClientSecureSocket - The
secureSocket
configuration
tcp: ClientSecureSocket
Secure Socket configuration for TCP Client.
Fields
- enable boolean(default true) - Enable SSL validation
- cert? TrustStore|string - Configurations associated with
crypto:TrustStore
or single certificate file that the client trusts
- ciphers? string[] - List of ciphers to be used
E.g.,
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
- handshakeTimeout? decimal - SSL handshake time out
- sessionTimeout? decimal - SSL session time out
tcp: ListenerConfiguration
Provides a set of configurations for tcp listener.
Fields
- localHost? string - The hostname
- secureSocket? ListenerSecureSocket - The SSL configurations for the listener
tcp: ListenerSecureSocket
Secure Socket configuration for TCP Listener.
Fields
- key KeyStore|CertKey - Configurations associated with
crypto:KeyStore
or combination of certificate and (PKCS8) private key of the server
- ciphers string[](default []) - List of ciphers to be used
E.g.,
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
- handshakeTimeout? decimal - SSL handshake time out
- sessionTimeout? decimal - SSL session time out
Errors
tcp: Error
Represents TCP module related errors.
Import
import ballerina/tcp;
Metadata
Released date: 15 days ago
Version: 1.12.0
License: Apache-2.0
Compatibility
Platform: java21
Ballerina version: 2201.11.0
GraalVM compatible: Yes
Pull count
Total: 10076
Current verison: 0
Weekly downloads
Keywords
network
socket
service
client
Contributors
Dependencies