udp
stableUDP socket plugin for sending and receiving datagrams. Provides a UdpSocket class with full control over binding, timeouts, broadcast, TTL, and blocking mode.
use plugin udp::{UdpSocket, send_to, recv, …} Functions (13)
- UdpSocket Creates a UDP socket and binds it to the given address.
- send_to Sends data as a string to the specified remote addr.
- recv Blocks until a datagram arrives and returns a table with data (string) and addr (source address string).
- local_addr Returns the local address the socket is bound to, e.g.
- set_nonblocking Switches the socket between blocking (default) and non-blocking mode.
- set_broadcast Enables or disables the SO_BROADCAST socket option, required before sending to broadcast addresses like 255.255.255.255.
- set_read_timeout Sets a read timeout in milliseconds.
- set_write_timeout Sets a write timeout in milliseconds.
- connect Associates a default remote address with the socket.
- send Sends data to the address previously set with connect.
- peek Reads up to buf_size bytes without removing the datagram from the queue.
- set_ttl Sets the IP Time-To-Live (hop count) for outgoing packets.
- ttl Returns the current IP TTL value set on the socket.
Creates a UDP socket and binds it to the given address.
Creates a UDP socket and binds it to the given address. Use "0.0.0.0:0" to bind to any available port, or specify a port like "0.0.0.0:9000".
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:9000")
print("Listening on {sock.local_addr()}")
Sends data as a string to the specified remote addr.
Sends data as a string to the specified remote addr. Returns the number of bytes sent.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:0")
let sent = sock.send_to("hello", "127.0.0.1:9000")
print("Sent {sent} bytes")
Blocks until a datagram arrives and returns a table with data (string) and addr (source address string).
Blocks until a datagram arrives and returns a table with data (string) and addr (source address string). The buf_size argument sets the maximum number of bytes to read.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:9000")
let msg = sock.recv(1024)
print("From {msg["addr"]}: {msg["data"]}")
Returns the local address the socket is bound to, e.g.
Returns the local address the socket is bound to, e.g. "0.0.0.0:9000".
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:0")
print(sock.local_addr())
Switches the socket between blocking (default) and non-blocking mode.
Switches the socket between blocking (default) and non-blocking mode. In non-blocking mode, recv returns immediately if no data is available.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:9000")
sock.set_nonblocking(true)
Enables or disables the SO_BROADCAST socket option, required before sending to broadcast addresses like 255.255.255.255.
Enables or disables the SO_BROADCAST socket option, required before sending to broadcast addresses like 255.255.255.255.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:0")
sock.set_broadcast(true)
sock.send_to("ping", "255.255.255.255:9001")
Sets a read timeout in milliseconds.
Sets a read timeout in milliseconds. Pass 0 or a negative value to remove the timeout (blocking forever). After the timeout elapses, recv will return an error.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:9000")
sock.set_read_timeout(5000)
Sets a write timeout in milliseconds.
Sets a write timeout in milliseconds. Pass 0 or a negative value to clear the timeout.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:0")
sock.set_write_timeout(3000)
Associates a default remote address with the socket.
Associates a default remote address with the socket. After calling this, you can use send and recv without specifying an address each time.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:0")
sock.connect("127.0.0.1:9000")
sock.send("ping")
Sends data to the address previously set with connect.
Sends data to the address previously set with connect. Returns the number of bytes sent.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:0")
sock.connect("127.0.0.1:9000")
let n = sock.send("hello world")
print("Sent {n} bytes")
Reads up to buf_size bytes without removing the datagram from the queue.
Reads up to buf_size bytes without removing the datagram from the queue. Returns the same #{"data": ..., "addr": ...} table as recv.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:9000")
let preview = sock.peek(64)
print("Peek from {preview["addr"]}: {preview["data"]}")
Sets the IP Time-To-Live (hop count) for outgoing packets.
Sets the IP Time-To-Live (hop count) for outgoing packets.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:0")
sock.set_ttl(64)
Returns the current IP TTL value set on the socket.
Returns the current IP TTL value set on the socket.
use plugin udp::{UdpSocket}
let sock = UdpSocket("0.0.0.0:0")
print("TTL: {sock.ttl()}")