Udp.open/port/send/recv/from_ip/from_port/close/resolve/local_ip/ip/ip_text, native BSD sockets (udp.ll) and Winsock (udp_win.ll, -lws2_32), linked only when a program uses Udp.*; example, docs and tests. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
27 lines
1,001 B
Text
27 lines
1,001 B
Text
# udp.ludic — Udp.* on the loopback: two sockets, one datagram each way.
|
|
program UdpDemo {
|
|
entry {
|
|
let a = Udp.open(0)
|
|
let b = Udp.open(0)
|
|
let pa = Udp.port(a)
|
|
let pb = Udp.port(b)
|
|
let lo = Udp.ip("127.0.0.1")
|
|
let out = Memory.bytes(16)
|
|
Memory.poke(out, 0, 104)
|
|
Memory.poke(out, 1, 105)
|
|
let sent = Udp.send(a, lo, pb, out, 2)
|
|
let inb = Memory.bytes(64)
|
|
var got = 0
|
|
var tries = 0
|
|
while got == 0 and tries < 200000 { got = Udp.recv(b, inb, 64); tries += 1 }
|
|
let from_ok = Udp.from_ip(b) == lo and Udp.from_port(b) == pa
|
|
Udp.send(b, Udp.from_ip(b), Udp.from_port(b), inb, got)
|
|
var back = 0
|
|
tries = 0
|
|
while back == 0 and tries < 200000 { back = Udp.recv(a, out, 16); tries += 1 }
|
|
let bad = Udp.ip("1.2.3") == 0 and Udp.ip("300.1.1.1") == 0
|
|
print(`{sent} {got} {Memory.peek(inb, 0)} {Memory.peek(inb, 1)} {from_ok} {back} {Udp.ip_text(lo)} {bad} {Udp.recv(a, out, 16)} {pa > 0}`)
|
|
Udp.close(a)
|
|
Udp.close(b)
|
|
}
|
|
}
|