ballerina/udp
Package Overview
This package provides an implementation for sending/receiving messages to/from another application process (local or remote) for connectionless protocols.
Client
The udp:Client
is used to interact with the remote UDP host and it can be defined as follows:
1import ballerina/udp;23public function main() returns error? {4 udp:Client socketClient = check new;56 udp:Datagram datagram = {7 remoteHost: "localhost",8 remotePort : 48829,9 data : "Hello Ballerina".toBytes()10 };1112 check socketClient->sendDatagram(datagram);1314 readonly & udp:Datagram result = check socketClient->receiveDatagram();1516 check socketClient->close();17}
ConnectClient
The udp:ConnectClient
is configured by providing remoteHost
and remotePort
; so that it only receives data from, and sends data to, the configured remote host. Once connected, data may not be received from or sent to any other hosts. The client remains connected until it is explicitly it is closed.
1import ballerina/udp;23public function main() returns error? {4 udp:ConnectClient socketClient = check new("localhost", 48829);56 string msg = "Hello Ballerina";7 check socketClient->writeBytes(msg.toBytes());89 readonly & byte[] result = check socketClient->readBytes();1011 check socketClient->close();12}
Listener
The udp:Listener
is used to listen to the incoming socket request.<br/>
The udp:Listener
can have following methods<br/>
onBytes(readonly & byte[] data, udp:Caller caller)
or onDatagram(readonly & udp:Datagram, udp:Caller)
- These remote method gets invoked once the content is received from the client. The client is represented using the udp:Caller
.<br/>
onError(readonly & udp:Error err)
- This remote method is invoked in an error situation.
A udp:Listener
can be defined as follows:
1import ballerina/udp;23service on new udp:Listener(48829) {4 remote function onBytes(readonly & byte[] data, udp:Caller caller)5 returns (readonly & byte[])|udp:Error? {6 // echo back the data to the same caller7 return data;8 }910 remote function onError(readonly & udp:Error err) {11 log:printError("An error occured", 'error = err);12 }13}
For information on the operations, which you can perform with this package, see the below Functions. For examples on the usage of the operations, see the following.
Listeners
[1]
Listener | This is used for creating UDP server endpoints. |
Clients
[3]
Caller | Represents caller object in UDP service remote methods. |
Client | Initializes the UDP connectionless client based on the provided configurations. |
ConnectClient | Initializes the UDP connection oriented client based on the provided configurations. |
Object Types
[1]
Service | Represent UDP Listener service type. |
Records
[4]
ClientConfiguration | Configurations for the connectionless UDP client. |
ConnectClientConfiguration | Configurations for the connection oriented udp client. |
Datagram | A self-contained, independent entity of data carrying sufficient information to be routed from the source to the destination nodes without reliance on earlier exchanges between the nodes and the transporting network. |
ListenerConfiguration | Represents the UDP listener configuration. |
Errors
[1]
Error | Represents udp module related errors. |