This API documentation is for Ballerina 1.2.0. View API documentation for the latest release.

Clients - socket : UdpClient

Initializes the UDP client based on the provided configurations.

Constructor

__init

(Address? localAddress, UdpClientConfig? config)

  • localAddress Address? ()
  • Local binding of the interface and port

Remote Methods

sendTo

Sends the given data to the specified remote client.

receiveFrom

Reads data from the remote client.

close

Closes the client socket connection.

Fields

  • localPort int (default 0)
  • The local port number to which this socket is bound

  • interface string? (default ())
  • The network interface to bind

  • id int (default 0)
  • A unique identifier to identify each client

sendTo

(byte[] content, Address address)

returns int | Error

Sends the given data to the specified remote client.

 int|socket:Error result = socketClient->sendTo(c1, {host: "localhost", port: 48826});

Parameters

  • content byte[]
  • The content to be sent to the client socket

  • address Address
  • The address of the remote client socket

  • Return Type

    (int | Error)
  • The number of bytes got written or else a socket:Error if the given data can't be sent

receiveFrom

(int length)

returns [byte[], int, Address] | ReadTimedOutError

Reads data from the remote client. If the data has the specified length, then it waits until that number of bytes are received from the client. Else, it returns the data available in the OS buffer or waits until the data is received. If the request length is lesser than the data in the buffer, then the rest will be discarded.

 [byte[], int, Address]|ReadTimedOutError result = socketClient->receiveFrom();

Parameters

  • length int (default -100)
  • Represents the number of bytes, which should be read

  • Return Type

    ([byte[], int, Address] | ReadTimedOutError)
  • The content as a byte array, the number of bytes read, the address of the sender, or else a socket:Error if the data can't be read from the client

close

()

returns Error?

Closes the client socket connection.

 socket:Error? closeResult = socketClient->close();
  • Return Type

    (Error?)
  • A socket:Error if it can't close the connection or else ()