Reference · UDP

udp.reply

signature
udp.reply(map, any) -> null | error(str)

What it does

Sends a packet back to where one came from: given the packet map that udp.listen handed to its function, it sends to packet.from from the port the packet came to, packet.port, as most protocols expect the answer. The data is as udp.send takes it: text, a list of byte values, or any other value as JSON, up to 65,507 bytes. It gives null, or an error value when the system refuses the packet. It needs the net effect.

Examples

in a program
udp.listen(9000, (p) => {    let cmd = json.parse(p.text)    match cmd.op {        "ping" => udp.reply(p, "pong"),        "get" => udp.reply(p, { value: state[cmd.key] }),        _ => udp.reply(p, { error: "unknown op" }),    }})

Edge cases

in a program
udp.listen(9000, (p) => {    channel.broadcast("readings", p.text)    udp.reply(p, [6])                          // one byte: ACK})

Errors

A first argument that is not a packet map (no port, no from), null data, data over 65,507 bytes, and a packet whose port the program does not listen on are runtime errors; the last is what a packet map made by hand in a program without udp.listen meets. A refused send is an error value.

this one fails
print(udp.reply({}, "x"))
output
line 1: `udp.reply` takes a packet that `udp.listen` gave, then what to send
this one fails
let packet = { text: "hi", bytes: [104, 105], from: { host: "127.0.0.1", port: 5000 }, port: 9000, at: now() }print(udp.reply(packet, "ok"))
output
line 2: `udp.reply`: the program does not listen on UDP port 9000

Chaining

The packet comes first, so inside the handler p | udp.reply("ok") reads as an answer; the result is null or an error, so ?? ignores a failure and is_err sees it.

in a program
udp.listen(9000, (p) => {    let r = p | udp.reply("ok")    if is_err(r) { print("could not answer", p.from.host, r.error) }})

See also

udp.listen, udp.send, json.parse, is_err

Thread
6Useful? Like it, or add to its thread below.

Thread 0 posts

On udp.reply(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo