Standard library
UDP
UDP bind, send, and receive.
Introduction
UDP bind, send, and receive. Import std/net/udp to bind the module as udp. Call free functions as udp.name(...). Struct methods use a receiver value.
/ std/net/udp
This page is the package reference for std/net/udp. Private helpers used only by co-located tests are not listed.
Struct · socket
UDP socket shape returned by udp.bind. The shape is exported as udp.socket. Methods on the receiver are listed next.
/ std/net/udp
; shape: udp.socket
$ sock = udp.bind("127.0.0.1:0")Parameters: No parameters. Type or shape export. Returns: Shape export used for literals and methods.
socket · send_to
Sends a datagram to the destination address string. Call form: socket.send_to(data, to).
/ std/net/udp
$ sock = udp.bind("127.0.0.1:0")
$ n = sock.send_to(b"hi", "127.0.0.1:9")Parameters: data: payload bytes or string. to: destination address string. Returns: Integer bytes sent, or a failure status from the runtime.
socket · recv_from
Receives a datagram and sender metadata. Limit caps the payload size. Call form: socket.recv_from(limit).
/ std/net/udp
$ sock = udp.bind("127.0.0.1:0")
$ msg = sock.recv_from(4096)Parameters: limit: maximum bytes to receive. Returns: Product with data and from address fields (empty data on failure).
socket · close
Closes the UDP socket and marks it closed. Call form: socket.close().
/ std/net/udp
$ sock = udp.bind("127.0.0.1:0")
sock.close()Parameters: No parameters. Returns: The socket receiver.
Functions
Free functions on udp. Each function has a short description, an example, then parameters and return shape.
bind
Binds a UDP socket on addr. Call form: udp.bind(addr).
/ std/net/udp
$ sock = udp.bind("127.0.0.1:0")Parameters: addr: bind address string. Returns: UDP socket product.
send_to
Sends a datagram to addr. Call form: udp.send_to(sock, data, addr).
/ std/net/udp
$ n = udp.send_to(sock, b"hi", "127.0.0.1:9")
Parameters: socket: UDP socket. data: payload. addr: destination address. Returns: Integer bytes sent, or -1 on failure.
recv_from
Receives a datagram and sender metadata. Call form: udp.recv_from(sock, limit).
/ std/net/udp
$ msg = udp.recv_from(sock, 4096)
Parameters: socket: UDP socket. limit: maximum bytes. Returns: Product with data and from address fields.
close
Closes a UDP socket. Call form: udp.close(sock).
/ std/net/udp
udp.close(sock)
Parameters: socket: UDP socket. Returns: None.