Core\Net::bindDatagram
Core\Net::bindDatagram(string $address, uint $port): Core\Net\DatagramCore\Net::bindDatagram( string $address, uint $port, ): Core\Net\Datagram
Opens a UDP socket on an address and a port. UDP sends short messages called datagrams. There is no connection: each message is sent on its own, to an address you name when you send it.
The result is a Core\Net\Datagram. Its send method sends one message, and its receive method waits for the next one. Port 0 lets the operating system pick a free port, and port tells you which one it picked.
The address must be an IP address, such as 127.0.0.1 or 0.0.0.0, and never a hostname. The exact address and port must be allowed under listen in the [capabilities.net] block of nvs.toml. Otherwise the call throws a RuntimeError.
Good to know: listen only allows the socket to open. To send a message, the host you send to must also be allowed under connect.
Parameters
$addressstringAn IP address, such as127.0.0.1,::1or0.0.0.0. A host name is not allowed, for the same reason as inCore\Net::listen: the grant names one address and port.$portuintThe port to use. With0, the operating system chooses a port, andCore\Net\Datagram::portreturns it.
Return value
Returns Core\Net\Datagram. A Core\Net\Datagram on that address and port. If your program does not close it, it is closed when the request ends.
Errors
RuntimeErrorThe configuration does not allow this address and port innet.listen. It is also thrown when$addressis not an IP address, or$portis not a valid port.IOErrorThe operating system did not allow the socket. The port is already in use, or the address does not belong to this host.
Examples
The examples below run with this configuration file.
# The examples open UDP sockets on this computer and send messages between them.# These lines allow a socket only at `127.0.0.1`, on a port the system picks.# A program run with `nvs run` may send to this computer without a grant.[capabilities.net]listen = ["127.0.0.1:0"]A socket on a free port
<?nvs// Shows how to open a UDP socket on a port the operating system picks.
// `Core\Net::bindDatagram` opens a UDP socket and returns a `Core\Net\Datagram`.// Port `0` means "pick any free port".Core\Net\Datagram $socket = Core\Net::bindDatagram('127.0.0.1', 0);
// `port()` returns the port that was picked. It is different on every run.// So this prints `bound: yes` and not the number.uint $port = $socket->port();echo 'bound: ', $port > 0 ? 'yes' : 'no', "\n";
$socket->close();bound: yesAn address that is not allowed
<?nvs// Shows the two ways `Core\Net::bindDatagram` can throw a `RuntimeError`.
// `localhost` is a hostname, not an IP address. So this call throws a `RuntimeError`.try { Core\Net::bindDatagram('localhost', 5353);} catch (RuntimeError $wrong) { echo $wrong->message, "\n";}
// The `nvs.toml` beside this example allows only `127.0.0.1` with port `0`.// Port 5353 is not allowed, so this call throws a `RuntimeError` too.try { Core\Net::bindDatagram('127.0.0.1', 5353);} catch (RuntimeError $refused) { echo $refused->message, "\n";}Core\Net::bindDatagram: `localhost` is not an IP address. A bind needs an endpoint, and a hostname is not oneCore\Net::bindDatagram needs the capability `net.listen` for 127.0.0.1:5353, which is not grantedhelp: add `listen = ["127.0.0.1:5353"]` under `[capabilities.net]` in nvs.tomlSending a counter to a metrics collector
<?nvs// Shows a program that sends counters to a metrics collector over UDP, the way StatsD clients do.
// The collector usually runs on another server. Here it is a second socket in the same program.Core\Net\Datagram $collector = Core\Net::bindDatagram('127.0.0.1', 0);Core\Net\Datagram $app = Core\Net::bindDatagram('127.0.0.1', 0);
// Each counter is one short message. `send` returns the number of bytes it sent.// The program does not wait for an answer, so a slow collector never slows it down.array<string> $lines = ['orders.created:1|c', 'orders.paid:1|c'];foreach ($lines as string $line) { $app->send('127.0.0.1', $collector->port(), $line as bytes, 1s);}
// The collector reads each message. This prints the two lines that were sent.// Text from the network is `tainted`, which means it is not checked yet.uint $i = 0;while ($i < 2) { Core\Net\Datagram\Message $message = $collector->receive(512, 5s); tainted string $text = $message->payload() as string; echo 'collector got: ', $text, "\n"; $i = $i + 1;}
$app->close();$collector->close();collector got: orders.created:1|ccollector got: orders.paid:1|c