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

public function main() returns error? {
  
    // Creates a new connectionless UDP client.
    // Optionally, you can provide the address that the socket needs to bind 
    // and the timeout in milliseconds, which specifies the read timeout value.
    // E.g.: `udp:Client client = new (localHost = "localhost", timeout = 5);`
    udp:Client socketClient = check new;

    string msg = "Hello Ballerina echo";
    udp:Datagram datagram = {
        remoteHost: "localhost",
        remotePort : 8080,
        data : msg.toBytes()
    };

    // Sends the data to the remote host.
    // The parameter is a Datagram record, which contains the `remoteHost`,
    // `remotePort`, and the `data` to be sent.
    check socketClient->sendDatagram(datagram);
    io:println("Datagram was sent to the remote host.");

    // Waits until the data is received from the remote host.
    readonly & udp:Datagram result = check socketClient->receiveDatagram();
    io:println("Received: ", string:fromBytes(result.data));        

    // Closes the client and releases the bound port.
    check socketClient->close();
}

UDP Connectionless Client

The UDP Client is used to send data to a specific remote host using the UDP protocol. This sample demonstrates how to send a datagram to a remote server and print the echoed response.

For more information on the underlying module, see the UDP module.

import ballerina/io;
import ballerina/udp;
public function main() returns error? {
  
    udp:Client socketClient = check new;

Creates a new connectionless UDP client. Optionally, you can provide the address that the socket needs to bind and the timeout in milliseconds, which specifies the read timeout value. E.g.: udp:Client client = new (localHost = "localhost", timeout = 5);

    string msg = "Hello Ballerina echo";
    udp:Datagram datagram = {
        remoteHost: "localhost",
        remotePort : 8080,
        data : msg.toBytes()
    };
    check socketClient->sendDatagram(datagram);
    io:println("Datagram was sent to the remote host.");

Sends the data to the remote host. The parameter is a Datagram record, which contains the remoteHost, remotePort, and the data to be sent.

    readonly & udp:Datagram result = check socketClient->receiveDatagram();
    io:println("Received: ", string:fromBytes(result.data));        

Waits until the data is received from the remote host.

    check socketClient->close();
}

Closes the client and releases the bound port.

bal run udp_client.bal
# This will print the output below upon a successful write.
Datagram was sent to the remote host.
# Print the response that is returned from the server as an echo.
Received: Hello Ballerina echo
import ballerina/io;
import ballerina/udp;

public function main() returns error? {
  
    // Creates a new connectionless UDP client.
    // Optionally, you can provide the address that the socket needs to bind 
    // and the timeout in milliseconds, which specifies the read timeout value.
    // E.g.: `udp:Client client = new (localHost = "localhost", timeout = 5);`
    udp:Client socketClient = check new;

    string msg = "Hello Ballerina echo";
    udp:Datagram datagram = {
        remoteHost: "localhost",
        remotePort : 8080,
        data : msg.toBytes()
    };

    // Sends the data to the remote host.
    // The parameter is a Datagram record, which contains the `remoteHost`,
    // `remotePort`, and the `data` to be sent.
    check socketClient->sendDatagram(datagram);
    io:println("Datagram was sent to the remote host.");

    // Waits until the data is received from the remote host.
    readonly & udp:Datagram result = check socketClient->receiveDatagram();
    io:println("Received: ", string:fromBytes(result.data));        

    // Closes the client and releases the bound port.
    check socketClient->close();
}

UDP Connectionless Client

The UDP Client is used to send data to a specific remote host using the UDP protocol. This sample demonstrates how to send a datagram to a remote server and print the echoed response.

For more information on the underlying module, see the UDP module.

import ballerina/io;
import ballerina/udp;
public function main() returns error? {
  
    udp:Client socketClient = check new;

Creates a new connectionless UDP client. Optionally, you can provide the address that the socket needs to bind and the timeout in milliseconds, which specifies the read timeout value. E.g.: udp:Client client = new (localHost = "localhost", timeout = 5);

    string msg = "Hello Ballerina echo";
    udp:Datagram datagram = {
        remoteHost: "localhost",
        remotePort : 8080,
        data : msg.toBytes()
    };
    check socketClient->sendDatagram(datagram);
    io:println("Datagram was sent to the remote host.");

Sends the data to the remote host. The parameter is a Datagram record, which contains the remoteHost, remotePort, and the data to be sent.

    readonly & udp:Datagram result = check socketClient->receiveDatagram();
    io:println("Received: ", string:fromBytes(result.data));        

Waits until the data is received from the remote host.

    check socketClient->close();
}

Closes the client and releases the bound port.

bal run udp_client.bal
# This will print the output below upon a successful write.
Datagram was sent to the remote host.
# Print the response that is returned from the server as an echo.
Received: Hello Ballerina echo

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