neon/udp

Types

Options for opening a UDP socket.

Create with new, then optionally configure with ip_address and ip_version before passing to open.

pub opaque type OpenOptions

Data received from a UDP socket, including the sender’s IP address, port, and the payload.

pub type ReceiveData {
  ReceiveData(
    ip_address: net.IpAddress,
    port: net.Port,
    payload: BitArray,
  )
}

Constructors

A UDP socket.

pub type Udp

Errors that can occur during UDP operations.

pub type UdpError {
  Closed
  Timeout
  SystemLimit
  Posix(net.Posix)
  InvalidPid
  UdpError(String)
}

Constructors

  • Closed

    The socket was closed.

  • Timeout

    The operation timed out.

  • SystemLimit

    The Erlang VM can’t allocate more resources for network operations.

  • Posix(net.Posix)

    A POSIX error.

  • InvalidPid

    The target pid is not alive.

  • UdpError(String)

    A generic UDP error with a description.

Values

pub fn close(socket: Udp) -> Nil

Closes a UDP socket.

This function is idempotent and always returns Nil.

pub fn connect(
  socket: Udp,
  address: net.Address,
  port: net.Port,
) -> Result(Nil, UdpError)

Associates a UDP socket with a remote address and port.

After connecting, send can be used without specifying a destination.

pub fn controlling_process(
  socket: Udp,
  pid: process.Pid,
) -> Result(Nil, UdpError)

Change the controlling process of a socket.

The controlling process is the process that the socket sends messages to.

pub fn ip_address(
  opts: OpenOptions,
  ip_address: net.IpAddress,
) -> OpenOptions

Binds the socket to a specific IP address.

When set, the IP version is derived from the address itself, and the ip_version option is ignored.

pub fn ip_version(
  opts: OpenOptions,
  ip_version: net.IpVersion,
) -> OpenOptions

Sets the IP version for the socket.

Only used when no IP address is set. When an IP address is provided, the version is derived from the address.

pub fn new(port: net.Port) -> OpenOptions

Creates open options for a UDP socket on the given port.

Defaults to IPv4 with no specific IP address binding.

pub fn open(opts: OpenOptions) -> Result(Udp, UdpError)

Opens a UDP socket with the given options.

pub fn port(socket: Udp) -> Result(net.Port, Nil)

Returns the port number assigned to a socket by the operating system.

Useful when opening on port 0 (OS-assigned).

pub fn receive(
  socket: Udp,
  length: Int,
  timeout: net.Timeout,
) -> Result(ReceiveData, UdpError)

Receives data from a UDP socket.

The length parameter specifies the number of bytes to receive. Use 0 to receive whatever data is available. Must be non-negative.

pub fn send(
  socket: Udp,
  payload: BitArray,
) -> Result(Nil, UdpError)

Sends data over a connected UDP socket.

Search Document