Rill v0.13 Reference

Standard library · The network

net/udp

Imported as import "net/udp" as udp, its names are then udp.…. Every signature below is the one the checker infers.

Types

Packet

  • Packet(from: Str, port: Int, data: Str)

Functions

fn open(port: Int) -> Result(Int, Str)

A socket bound to port on every interface; 0 for one the system picks.

s = udp.open(0)?
udp.port_of(s) > 0   # => true
sock_close(s)

fn port_of(fd: Int) -> Int

s = udp.open(18911)?
udp.port_of(s)   # => 18911
sock_close(s)

fn send_to(fd: Int, host: Str, port: Int, data: Str) -> Int

Sends data as one datagram; the bytes sent, or -1.

a = udp.open(0)?
b = udp.open(0)?
udp.send_to(a, "127.0.0.1", udp.port_of(b), "hello")   # => 5
sock_close(a)
sock_close(b)

fn recv_from(fd: Int) -> Packet

The next datagram, whole, with where it came from.

a = udp.open(0)?
b = udp.open(0)?
udp.send_to(a, "127.0.0.1", udp.port_of(b), "one")
udp.recv_from(b).data   # => one
sock_close(a)
sock_close(b)

Tests

  • test_a_datagram_goes_round
  • test_too_long_and_bad_address
  • test_a_strand_waits_for_it