Grav v1.14.0

UDP

import "std/net" (not included in import "std") · namespace std.net

UDP sends separate messages (datagrams) without setting up a connection. It is lighter than TCP, but each datagram may be lost, duplicated or arrive out of order, and is limited to about 64 KB. It suits discovery protocols, metrics, games and anything where an occasional lost message is fine.

grav
import "std"
import "std/net"

fn main() {
    let server = UdpSocket.bind("127.0.0.1", 0)     // port 0: any free port
    server.setTimeoutMs(2000)
    let client = UdpSocket.open()
    client.setTimeoutMs(2000)

    client.sendTo("127.0.0.1", server.port(), "ping")
    let p = server.receive()
    print("${p.text()} from port ${p.port() == client.port()}")
    server.sendTo(p.host(), p.port(), "pong")
    print(client.receive().text())

    server.close()
    client.close()
}
Output
ping from port true
pong
  • UdpSocket.bind(host, port) listens on an address; UdpSocket.open() makes a socket for sending (on a free port, so replies can come back).
  • sendTo and sendBytesTo send one datagram; receive() waits for the next and returns a UdpPacket with the data and the sender's host() and port().
  • setTimeoutMs(ms) makes receive() throw TimeoutError instead of waiting forever; setBroadcast(true) allows broadcast addresses.
  • Failures throw NetError.

API

class UdpPacket

One received datagram: its contents and who sent it.

Constructors

  • new UdpPacket(data: Bytes, from: string)

Fields

  • readonly data: Bytes — The bytes received.
  • readonly from: string — The sender as "host:port" ("[host]:port" for IPv6).

Methods

  • fn text() -> string — The contents as text.
  • fn host() -> string — The sender's address, without the port.
  • fn port() -> int — The sender's port.
  • fn toString() -> string — As text: "from: contents".

class UdpSocket

A UDP socket: send datagrams to any address, receive them on its port.

Constructors

  • new UdpSocket(fd: int)

Static members

  • static fn bind(host: string, port: int) -> UdpSocket — A socket bound to host:port ("" or "*" for every interface; port 0 picks a free one).
  • static fn bind(port: int) -> UdpSocket — A socket on every interface at port.
  • static fn open() -> UdpSocket — A socket for sending (bound to a free port, so replies can come back).

Methods

  • fn port() -> int — The local port.
  • fn sendTo(host: string, port: int, text: string) — Sends text as one datagram to host:port.
  • fn sendBytesTo(host: string, port: int, data: Bytes) — Sends raw bytes as one datagram to host:port.
  • fn receive() -> UdpPacket — Waits for the next datagram (TimeoutError after setTimeoutMs, NetError on failure).
  • fn setTimeoutMs(ms: int) — Makes receive() give up after ms milliseconds (0: wait forever).
  • fn setBroadcast(on: bool) — Allows sending to broadcast addresses such as 255.255.255.255.
  • fn close() — Closes the socket (safe to call twice).
  • fn isClosed() -> bool — Whether close() has been called.

Edit this page on GitHub