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;
2
3public function main() returns error? {
4 udp:Client socketClient = check new;
5
6 udp:Datagram datagram = {
7 remoteHost: "localhost",
8 remotePort : 48829,
9 data : "Hello Ballerina".toBytes()
10 };
11
12 check socketClient->sendDatagram(datagram);
13
14 readonly & udp:Datagram result = check socketClient->receiveDatagram();
15
16 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;
2
3public function main() returns error? {
4 udp:ConnectClient socketClient = check new("localhost", 48829);
5
6 string msg = "Hello Ballerina";
7 check socketClient->writeBytes(msg.toBytes());
8
9 readonly & byte[] result = check socketClient->readBytes();
10
11 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

  • 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.
  • onError(readonly & udp:Error err) - This remote method is invoked in an error situation.

A udp:Listenercan be defined as follows:

1import ballerina/udp;
2import ballerina/log;
3
4service on new udp:Listener(48829) {
5 remote function onDatagram(readonly & udp:Datagram datagram)
6 returns udp:Datagram|udp:Error? {
7 // echo back the data to the same caller
8 return datagram;
9 }
10
11 remote function onError(udp:Error err) {
12 log:printError("An error occured", 'error = err);
13 }
14}

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.