Standard library · Network

net/udp

Sending and receiving UDP packets, with broadcast and multicast.

import net.udp
On this page
  1. Types
  2. Functions

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
A UDP socket.
NameTypeDescription
fdi64 = -1The socket descriptor.
net-udp-Echo.salam
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)
end
Outputtrue true true 5 hello 127.0.0.1 true two 5 127.0.0.1 [] true true true

Packet

struct Packet
One received packet.
NameTypeDescription
datastr = ""The contents; empty when nothing arrived within the timeout.
from_hoststr = ""The address of the sender.
from_portint = 0The 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): Socket
Makes a socket that listens on the given port on every address; zero means a free port.

The example for this item is shown with the ones above: net-udp-Echo.salam

BindOn

func BindOn(host: str, port: int): Socket
Like Bind, but on the given address only.

The example for this item is shown with the ones above: net-udp-Echo.salam

Open

func Open(): Socket
A socket with no particular port, for sending.

The example for this item is shown with the ones above: net-udp-Echo.salam

Port

func Port(s: Socket): int
The port of the socket.

The example for this item is shown with the ones above: net-udp-Echo.salam

Ok

func Ok(s: Socket): bool
True when the socket was created properly.

The example for this item is shown with the ones above: net-udp-Echo.salam

Close

func Close(s: Socket)
Closes the 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): int
Sends one packet to a host and port and returns the number of bytes.

The 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): int
The same as Send.

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): int
Sends n 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): Packet
Receives one packet; when a timeout is set and nothing arrives, an empty packet.

The 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): int
Reads the packet into memory and gives back the address and port of the sender.

The example for this item is shown with the ones above: net-udp-Echo.salam

SetBroadcast

func SetBroadcast(s: Socket, enable: bool)
Allows sending to the broadcast address of the network.

The example for this item is shown with the ones above: net-udp-Echo.salam

SetTimeout

func SetTimeout(s: Socket, millis: int)
How many milliseconds a receive may wait at most.

The example for this item is shown with the ones above: net-udp-Echo.salam

JoinMulticast

func JoinMulticast(s: Socket, group_ip: str): bool
Joins a multicast group so its packets arrive.

The example for this item is shown with the ones above: net-udp-Echo.salam

LeaveMulticast

func LeaveMulticast(s: Socket, group_ip: str): bool
Leaves a multicast group.

The example for this item is shown with the ones above: net-udp-Echo.salam