Module udp
API

ballerina/udp Ballerina library
Clients
udp: Caller
Represents caller object in UDP service remote methods.
sendBytes
function sendBytes(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 a
udp:Error
if the given data can't be sent
sendDatagram
Sends the response as datagram to a remote destination as specified in datagram.
Parameters
- datagram Datagram - Contains the data to be sent to the remote host and the address of the remote host
Return Type
- Error? - () or else a
udp:Error
if the given data can't be sent
udp: Client
Initializes the UDP connectionless client based on the provided configurations.
Constructor
Initializes the UDP connectionless client based on the provided configurations.
udp:Client|udp:Error? socketClient = new(localHost = "localhost");
init (*ClientConfiguration config)
- config *ClientConfiguration - Connectionless client-related configuration
sendDatagram
Sends the given data to the specified remote host.
udp:Error? result = socketClient->sendDatagram({remoteHost: "localhost", remotePort: 48826, data:"msg".toBytes()});
Parameters
- datagram Datagram - Contains the data to be sent to the remote host and the address of the remote host
Return Type
- Error? -
()
or else audp:Error
if the given data cannot be sent
receiveDatagram
Reads data from the remote host.
udp:Datagram|udp:Error result = socketClient->receiveDatagram();
Return Type
close
function close() returns Error?
Free up the occupied socket.
udp:Error? closeResult = socketClient->close();
Return Type
- Error? - A
udp:Error
if it can't close the connection or else()
udp: ConnectClient
Initializes the UDP connection oriented client based on the provided configurations.
Constructor
Initializes the UDP connect client based on the provided configurations.
udp:ConnectClient|udp:Error? socketClient = new("www.remote.com", 80,
localHost = "localHost");
init (string remoteHost, int remotePort, *ConnectClientConfiguration config)
- remoteHost string - The hostname or the IP address of the remote host
- remotePort int - The port number of the remote host
- config *ConnectClientConfiguration - Connection oriented client related configuration
writeBytes
function writeBytes(byte[] data) returns Error?
Sends the given data to the connected remote host.
udp:Error? result = socketClient->writeBytes("msg".toBytes());
Parameters
- data byte[] - The data need to be sent to the connected remote host
Return Type
- Error? -
()
or else audp:Error
if the given data can't be sent
readBytes
function readBytes() returns (readonly & byte[])|Error
Reads data only from the connected remote host.
readonly & byte[]|udp:Error result = socketClient->readBytes();
Return Type
- (readonly & byte[])|Error - A
byte[]
or else audp:Error
if the data cannot be read from the remote host
close
function close() returns Error?
Free up the occupied socket.
udp:Error? closeResult = socketClient->close();
Return Type
- Error? - A
udp:Error
if it can't close the connection or else()
Service types
udp: Service
Represent UDP Listener service type.
Listeners
udp: Listener
This is used for creating the UDP server endpoints. A UDP server endpoint
is capable of responding to remote callers. The udp:Listener
is
responsible for initializing the endpoint using the provided
configurations.
Constructor
Initializes the UDP listener based on the privovided configurations.
listener Listener|error? udpServer = new (8080);
init (int localPort, *ListenerConfiguration config)
- localPort int - The port number of the remote service
- config *ListenerConfiguration - Configurations related to the
udp:Listener
attach
Binds a service to the udp:Listener
.
udp:error? result = udpListener.attach(helloService);
Return Type
- error? -
()
or else audp: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 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 udp:Listener
.
udp:error? result = udpListener.detach(helloService);
Parameters
- s Service - Type descriptor of the service
Return Type
- error? -
()
or else audp:Error
upon failure to detach the service
Records
udp: ClientConfiguration
Configurations for the connectionless UDP client.
Fields
- 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
- localHost? string - Local binding of the interface
udp: ConnectClientConfiguration
Configurations for the connection oriented udp client.
Fields
- 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
- localHost? string - Local binding of the interface
udp: 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.
Fields
- remoteHost string - The hostname or the IP address of the remote host
- remotePort int - The port number of the remote host
- data byte[] - The content which needs to be transported to the remote host
udp: ListenerConfiguration
Represents the UDP listener configuration.
Fields
- remoteHost? string - The hostname or the IP address of the remote host
- remotePort? int - The remote host's port number. If this is not set, the server runs without connecting to a remote host
- localHost? string - The interface for the server to be bound
Errors
udp: Error
Represents udp module related errors.
Import
import ballerina/udp;
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: 3602
Current verison: 0
Weekly downloads
Keywords
UDP
datagram
transport
Contributors