import ballerina/io;
import ballerina/socket;public function main() {
    socket:UdpClient socketClient = new;
    string msg = "Hello from UDP client";
    byte[] c1 = msg.toBytes();
    var sendResult =
        socketClient->sendTo(c1, {host: "localhost", port: 48826});
    if (sendResult is int) {
        io:println("Number of bytes written: ", sendResult);
    } else {
        error e = sendResult;
        panic e;
    }
    var result = socketClient->receiveFrom();
    if (result is [byte[], int, socket:Address]) {
        var [content, length, address] = result;
        var byteChannel = io:createReadableChannel(content);
        if (byteChannel is io:ReadableByteChannel) {
            io:ReadableCharacterChannel characterChannel =
                new io:ReadableCharacterChannel(byteChannel, "UTF-8");
            var str = characterChannel.read(60);
            if (str is string) {
                io:println("Received: ", <@untainted>str);
            } else {
                io:println("Error: ", str.detail()?.message);
            }
        }
    } else {
        io:println("An error occurred while receiving the data ",
            result);
    }
    var closeResult = socketClient->close();
    if (closeResult is error) {
        io:println("An error occurred while closing the connection ",
            closeResult);
    }
}# To run the client, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run udp_socket_client.bal# This will print the below output upon a successful write.
Number of bytes written: 21
# Print the response that is returned from the server as an echo.
Received: Hello from UDP client

Basic UDP Client Socket

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

import ballerina/io;
import ballerina/socket;

This is the client implementation for the UDP socket.

public function main() {
    socket:UdpClient socketClient = new;
    string msg = "Hello from UDP client";
    byte[] c1 = msg.toBytes();

Create a new socket client. Optionally, you can provide port that this socket need to bind or both interface and port as follows. socket:UdpClient client = new(localAddress = { port: 48828 }); socket:UdpClient client = new(localAddress = { host: “localhost”, port: 48828 });

    var sendResult =
        socketClient->sendTo(c1, {host: "localhost", port: 48826});
    if (sendResult is int) {
        io:println("Number of bytes written: ", sendResult);
    } else {
        error e = sendResult;
        panic e;
    }

Send data to remote host. Second parameter is the address of the remote host.

    var result = socketClient->receiveFrom();
    if (result is [byte[], int, socket:Address]) {
        var [content, length, address] = result;
        var byteChannel = io:createReadableChannel(content);
        if (byteChannel is io:ReadableByteChannel) {
            io:ReadableCharacterChannel characterChannel =
                new io:ReadableCharacterChannel(byteChannel, "UTF-8");
            var str = characterChannel.read(60);
            if (str is string) {
                io:println("Received: ", <@untainted>str);
            } else {
                io:println("Error: ", str.detail()?.message);
            }
        }
    } else {
        io:println("An error occurred while receiving the data ",
            result);
    }

Wait until data receive from remote host. This will block until receive at least a single byte. Optionally, you can specify the length as below. socketClient->receiveFrom(length = 30) This will block until specified length of bytes receive from host.

    var closeResult = socketClient->close();
    if (closeResult is error) {
        io:println("An error occurred while closing the connection ",
            closeResult);
    }
}

Close the client and release the bound port.

# To run the client, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run udp_socket_client.bal
# This will print the below output upon a successful write.
Number of bytes written: 21
# Print the response that is returned from the server as an echo.
Received: Hello from UDP client