net/udp
Sending and receiving UDP packets, with broadcast and multicast.
import net.udp
UDP sends separate packets, with no connection and no guarantee that they arrive or arrive in order; in exchange it is fast and simple. It is what DNS, games, live streaming and discovering devices on a local network use. Up to about 1500 bytes per packet is safe.
Types
Socket
struct Socket| Name | Type | Description |
|---|---|---|
fd | i64 = -1 | The socket descriptor. |
import net.udp
import mem
func main:
receiver := udp.BindOn("127.0.0.1", 0)
sender := udp.Open()
println udp.Ok(receiver), udp.Ok(sender), udp.Port(receiver) > 0
udp.SetTimeout(receiver, 2000)
println udp.Send(sender, "127.0.0.1", udp.Port(receiver), "hello")
p := udp.Receive(receiver, 1500)
println p.data, p.from_host, p.from_port > 0
_ := udp.SendTo(sender, "127.0.0.1", udp.Port(receiver), "two")
text := "three"
_ := udp.SendBytes(sender, "127.0.0.1", udp.Port(receiver), text as void*, text.len())
println udp.Receive(receiver, 1500).data
buffer := mem.AllocateZeroed(64 as u64)
mut host := ""
mut port := 0
println udp.ReceiveBytes(receiver, buffer, 64, host, port), host
udp.SetTimeout(receiver, 100)
println "[" + udp.Receive(receiver, 1500).data + "]"
udp.SetBroadcast(sender, true)
println udp.JoinMulticast(receiver, "239.1.2.3") or true, udp.LeaveMulticast(receiver, "239.1.2.3") or true
other := udp.Bind(0)
println udp.Ok(other)
udp.Close(other)
udp.Close(receiver)
udp.Close(sender)
mem.Free(buffer)
endPacket
struct Packet| Name | Type | Description |
|---|---|---|
data | str = "" | The contents; empty when nothing arrived within the timeout. |
from_host | str = "" | The address of the sender. |
from_port | int = 0 | The port of the sender. |
The example for this item is shown with the ones above: net-udp-Echo.salam
Functions
Bind
func Bind(port: int): SocketThe example for this item is shown with the ones above: net-udp-Echo.salam
BindOn
func BindOn(host: str, port: int): SocketBind, but on the given address only.The example for this item is shown with the ones above: net-udp-Echo.salam
Open
func Open(): SocketThe example for this item is shown with the ones above: net-udp-Echo.salam
Port
func Port(s: Socket): intThe example for this item is shown with the ones above: net-udp-Echo.salam
Ok
func Ok(s: Socket): boolThe example for this item is shown with the ones above: net-udp-Echo.salam
Close
func Close(s: Socket)The example for this item is shown with the ones above: net-udp-Echo.salam
Send
func Send(s: Socket, host: str, port: int, data: str): intThe example for this item is shown with the ones above: net-udp-Echo.salam
SendTo
func SendTo(s: Socket, host: str, port: int, data: str): intSend.The example for this item is shown with the ones above: net-udp-Echo.salam
SendBytes
func SendBytes(s: Socket, host: str, port: int, ptr: void*, n: int): intn bytes from memory in one packet.The example for this item is shown with the ones above: net-udp-Echo.salam
Receive
func Receive(s: Socket, max: int): PacketThe example for this item is shown with the ones above: net-udp-Echo.salam
ReceiveBytes
func ReceiveBytes(s: Socket, buf: void*, max: int, out_host &: str, out_port &: int): intThe example for this item is shown with the ones above: net-udp-Echo.salam
SetBroadcast
func SetBroadcast(s: Socket, enable: bool)The example for this item is shown with the ones above: net-udp-Echo.salam
SetTimeout
func SetTimeout(s: Socket, millis: int)The example for this item is shown with the ones above: net-udp-Echo.salam
JoinMulticast
func JoinMulticast(s: Socket, group_ip: str): boolThe example for this item is shown with the ones above: net-udp-Echo.salam
LeaveMulticast
func LeaveMulticast(s: Socket, group_ip: str): boolThe example for this item is shown with the ones above: net-udp-Echo.salam