Swan Lake Beta3 is here.
import ballerina/io;
import ballerina/udp;

// You can combine a listener declaration with a service declaration as shown in this example.
service on new udp:Listener(8080) {
    remote function onDatagram(readonly & udp:Datagram dg) {
        io:println("bytes received: ", dg.data.length());
    }
}

Service Declaration

A service represents a collection of remotely accessible methods attached to a particular listener. A service declaration is syntactic sugar for creating services in Ballerina. A service declaration gets desugared into several things including creating a listener object, registering it with the module, creating a service object, attaching the service object to the listener object, etc,. The type of the listener determines required type of remote methods.

import ballerina/io;
import ballerina/udp;
service on new udp:Listener(8080) {
    remote function onDatagram(readonly & udp:Datagram dg) {
        io:println("bytes received: ", dg.data.length());
    }
}

You can combine a listener declaration with a service declaration as shown in this example.

bal run service_declaration.bal
bytes received: 17
# Use the following Netcat (or nc) command to send packets to the service. 
nc -u localhost 8080
Hello, Ballerina
import ballerina/io;
import ballerina/udp;

// You can combine a listener declaration with a service declaration as shown in this example.
service on new udp:Listener(8080) {
    remote function onDatagram(readonly & udp:Datagram dg) {
        io:println("bytes received: ", dg.data.length());
    }
}

Service Declaration

A service represents a collection of remotely accessible methods attached to a particular listener. A service declaration is syntactic sugar for creating services in Ballerina. A service declaration gets desugared into several things including creating a listener object, registering it with the module, creating a service object, attaching the service object to the listener object, etc,. The type of the listener determines required type of remote methods.

import ballerina/io;
import ballerina/udp;
service on new udp:Listener(8080) {
    remote function onDatagram(readonly & udp:Datagram dg) {
        io:println("bytes received: ", dg.data.length());
    }
}

You can combine a listener declaration with a service declaration as shown in this example.

bal run service_declaration.bal
bytes received: 17
# Use the following Netcat (or nc) command to send packets to the service. 
nc -u localhost 8080
Hello, Ballerina
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand