Skip to content

Core\Net\Datagram::send

Sends one message over UDP to a host and a port. The message is sent whole, as one datagram, and the method returns how many bytes it sent.

send does not wait for an answer and does not check that the message arrived. UDP gives no such promise. If the other side must answer, it sends its own message back, and you read it with receive.

Your program may send to any public host with no nvs.toml. A connect list in the [capabilities.net] block allows only the hosts it names. A host that is not allowed throws a RuntimeError. A message that is too large for one datagram throws an IOError, and nothing is sent.

Parameters

  • $hoststringA host name or an IP address. It is checked against net.connect before its address is looked up. The message goes to the one address that is found.
  • $portuintThe port to send to, 1 to 65535.
  • $payloadbytesThe bytes of one message. A message is never sent in parts. A message that is too large for the network throws an error and is not split.
  • $withinCore\Time\DurationHow long to wait until the socket accepts the message. This limit is for this call only.

Return value

Returns uint. How many bytes were sent. When the call succeeds, this is always the length of $payload.

Errors

  • RuntimeErrorThe configuration does not allow this host in net.connect. It is also thrown when the host has no address, or its address is in a denied range. It is also thrown when $port is not a valid port, this handle is closed, or $within is not a positive length of time.
  • TimeoutErrorThe socket did not accept the message within $within.
  • IOErrorThe operating system did not allow the send. The message is larger than the maximum size, or the network cannot be reached.

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`, and messages only to `127.0.0.1`.
# Without the `connect` line, a program run with `nvs run` may send to any public host.
[capabilities.net]
listen = ["127.0.0.1:0"]
connect = ["127.0.0.1"]

Sending one message

<?nvs
// Shows how to send one message from one UDP socket to another.
Core\Net\Datagram $sender = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $receiver = Core\Net::bindDatagram('127.0.0.1', 0);
// `send` needs the host, the port, the message as `bytes`, and how long it may wait.
// It returns the number of bytes it sent. This prints `sent: 5`.
uint $sent = $sender->send('127.0.0.1', $receiver->port(), 'hello' as bytes, 1s);
echo 'sent: ', $sent, "\n";
// The receiver gets the whole message. This prints `received: hello`.
echo 'received: ', $receiver->receive(64, 5s)->payload() as string, "\n";
$sender->close();
$receiver->close();
Output
sent: 5
received: hello

A host that is not allowed

<?nvs
// Shows that `send` throws a `RuntimeError` when the host is not allowed in `nvs.toml`.
Core\Net\Datagram $socket = Core\Net::bindDatagram('127.0.0.1', 0);
// The `nvs.toml` beside this example allows sending only to `127.0.0.1`.
// `10.0.0.5` is not in that list, so nothing is sent and the call throws.
try {
$socket->send('10.0.0.5', 514, 'hello' as bytes, 1s);
} catch (RuntimeError $refused) {
echo $refused->message, "\n";
}
$socket->close();
Output
Core\Net\Datagram::send needs the capability `net.connect` for 10.0.0.5, which is not granted
help: add `connect = ["10.0.0.5"]` under `[capabilities.net]` in nvs.toml

Sending a log line to syslog

<?nvs
// Shows a program that sends its log lines to a syslog server over UDP.
// The syslog server usually runs on another machine. Here it is a second socket in the same program.
Core\Net\Datagram $syslog = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $app = Core\Net::bindDatagram('127.0.0.1', 0);
// A syslog line starts with a priority in angle brackets. `<14>` means "an information message".
// Each line is one message, so the server never gets half a line.
array<string> $events = ['order 1042 paid', 'order 1042 shipped'];
foreach ($events as string $event) {
$app->send('127.0.0.1', $syslog->port(), ('<14>shop: ' . $event) as bytes, 1s);
}
// The server reads the two lines. Text from the network is `tainted`, which means it is not checked yet.
uint $i = 0;
while ($i < 2) {
tainted string $line = $syslog->receive(1024, 5s)->payload() as string;
echo 'syslog got: ', $line, "\n";
$i = $i + 1;
}
$app->close();
$syslog->close();
Output
syslog got: <14>shop: order 1042 paid
syslog got: <14>shop: order 1042 shipped