TCP
Service
tcp_listener_tls.bal
import ballerina/io;
import ballerina/tcp;
listener tcp:Listener securedEP = check new (3000,
secureSocket = {
key: {
certFile: "./resources/public.crt",
keyFile: "./resources/private.key"
}
}
);
isolated service on securedEP {
isolated remote function onConnect(tcp:Caller caller) returns tcp:ConnectionService {
io:println("Client connected on server port: ", caller.remotePort);
return new EchoService();
}
}
isolated service class EchoService {
*tcp:ConnectionService;
isolated remote function onBytes(readonly & byte[] data) returns byte[] {
io:println("Received message: ", string:fromBytes(data));
return data;
}
}
$ bal run tcp_listener_tls.bal
Client
tcp_client_tls.bal
import ballerina/io;
import ballerina/tcp;
final tcp:Client securedEP = check new ("localhost", 3000,
secureSocket = {
cert: "./resources/public.crt"
}
);
public function main() returns error? {
check securedEP->writeBytes("Hello, World!".toBytes());
readonly & byte[] receivedData = check securedEP->readBytes();
io:println("Received message: ", string:fromBytes(receivedData));
check securedEP->close();
}
$ bal run tcp_client_tls.bal
Resources
public.crt
-----BEGIN CERTIFICATE-----
MIIDdzCCAl+gAwIBAgIEfP3e8zANBgkqhkiG9w0BAQsFADBkMQswCQYDVQQGEwJV
UzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxDTALBgNVBAoT
BFdTTzIxDTALBgNVBAsTBFdTTzIxEjAQBgNVBAMTCWxvY2FsaG9zdDAeFw0xNzEw
MjQwNTQ3NThaFw0zNzEwMTkwNTQ3NThaMGQxCzAJBgNVBAYTAlVTMQswCQYDVQQI
EwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzENMAsGA1UEChMEV1NPMjENMAsG
A1UECxMEV1NPMjESMBAGA1UEAxMJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF
AAOCAQ8AMIIBCgKCAQEAgVyi6fViVLiZKEnw59xzNi1lcYh6z9dZnug+F9gKqFIg
mdcPe+qtS7gZc1jYTjWMCbx13sFLkZqNHeDUadpmtKo3TDduOl1sqM6cz3yXb6L3
4k/leh50mzIPNm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