Skip to content

Core\Net\Datagram::receive

Waits for one message on a UDP socket and returns it as a Core\Net\Datagram\Message. The message has three parts: the bytes that arrived, and the host and the port of the program that sent them. You use that host and port to send a reply.

$max is the largest number of bytes you want to keep. A longer message is cut at $max, and the rest of it is lost. No UDP message is larger than 64 KiB, so receive never keeps more than that.

$within is how long receive waits. If no message arrives in that time, it throws a TimeoutError. The bytes are tainted, because they come from the network. Receiving needs no setting in nvs.toml of its own. The permission to open the socket is enough.

Parameters

  • $maxuintHow many bytes to keep. The limit is 64 KiB, because no message is larger. A message arrives whole or not at all. If it is longer than $max, only the first $max bytes are kept, and the rest is lost. No later call returns the rest.
  • $withinCore\Time\DurationHow long to wait for a message. This limit is for this call only.

Return value

Returns Core\Net\Datagram\Message. A Core\Net\Datagram\Message with the bytes, and the address and port they came from.

Errors

  • RuntimeErrorThis handle is closed, or $within is not a positive length of time.
  • TimeoutErrorNo datagram arrived within $within.
  • IOErrorThe operating system could not receive. On Windows, this error also reports that an earlier send from this socket could not reach its address.

Examples

The examples below run with this configuration file.

nvs.toml
# 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"]

Receiving one message

<?nvs
// Shows how to wait for one UDP message and see who sent it.
Core\Net\Datagram $server = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $client = Core\Net::bindDatagram('127.0.0.1', 0);
$client->send('127.0.0.1', $server->port(), 'ping' as bytes, 1s);
// `receive` keeps at most 64 bytes and waits at most 5 seconds.
// It returns a `Core\Net\Datagram\Message`.
Core\Net\Datagram\Message $message = $server->receive(64, 5s);
// This prints `payload: ping` and `from: 127.0.0.1`.
echo 'payload: ', $message->payload() as string, "\n";
echo 'from: ', $message->host(), "\n";
// The message came from the port of `$client`. This prints `from the client: yes`.
echo 'from the client: ', $message->port() == $client->port() ? 'yes' : 'no', "\n";
$client->close();
$server->close();
Output
payload: ping
from: 127.0.0.1
from the client: yes

A message longer than max

<?nvs
// Shows what `receive` keeps when a message is longer than `$max`.
Core\Net\Datagram $server = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $client = Core\Net::bindDatagram('127.0.0.1', 0);
$client->send('127.0.0.1', $server->port(), 'hello world' as bytes, 1s);
$client->send('127.0.0.1', $server->port(), 'second' as bytes, 1s);
// The first message has 11 bytes, and `$max` is 5. This prints `kept: hello`.
echo 'kept: ', $server->receive(5, 5s)->payload() as string, "\n";
// The other 6 bytes are lost. The next call returns the next message.
// This prints `next: second`.
echo 'next: ', $server->receive(64, 5s)->payload() as string, "\n";
$client->close();
$server->close();
Output
kept: hello
next: second

A server that replies to each sender

<?nvs
// Shows a small UDP server that answers each message and stops when no more messages arrive.
Core\Net\Datagram $server = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $first = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $second = Core\Net::bindDatagram('127.0.0.1', 0);
$first->send('127.0.0.1', $server->port(), 'status' as bytes, 1s);
$second->send('127.0.0.1', $server->port(), 'version' as bytes, 1s);
// The server waits up to 200 milliseconds for each message.
// When none arrives in that time, `receive` throws a `TimeoutError` and the loop stops.
uint $answered = 0;
bool $running = true;
while ($running) {
try {
Core\Net\Datagram\Message $request = $server->receive(512, 200ms);
// `host()` and `port()` are the address of the sender. The reply goes there.
tainted bytes $reply = ('ok: ' . ($request->payload() as string)) as bytes;
$server->send($request->host(), $request->port(), $reply, 1s);
$answered = $answered + 1;
} catch (TimeoutError $late) {
$running = false;
}
}
// This prints `answered: 2`.
echo 'answered: ', $answered, "\n";
// Each client gets the reply to its own message.
// This prints `ok: status` and then `ok: version`.
echo $first->receive(512, 5s)->payload() as string, "\n";
echo $second->receive(512, 5s)->payload() as string, "\n";
$first->close();
$second->close();
$server->close();
Output
answered: 2
ok: status
ok: version