Skip to content

Core\Net\Datagram::close

$datagram->close(): void

Closes a UDP socket. The port is free again, and other programs can use it.

After close, every method of the socket throws a RuntimeError, and so does a second close. Messages you already received stay readable, because each Core\Net\Datagram\Message has its own copy of what arrived.

If your program does not call close, the socket is closed when the request ends. Call it yourself when you are done with a socket, so the port is free sooner. A finally block is a good place for it, because it runs even when an error is thrown.

Return value

Returns void. Nothing.

Errors

  • RuntimeErrorThis handle is already closed.

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"]

Closing when done

<?nvs
// Shows how to close a UDP socket when you are done with 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(), 'last message' as bytes, 1s);
Core\Net\Datagram\Message $message = $server->receive(64, 5s);
// `close` frees the port of each socket.
$client->close();
$server->close();
// The message has its own copy of the bytes, so it is still readable.
// This prints `still readable: last message`.
echo 'still readable: ', $message->payload() as string, "\n";
Output
still readable: last message

Using a closed socket

<?nvs
// Shows that a closed socket throws a `RuntimeError` when you use it again.
Core\Net\Datagram $socket = Core\Net::bindDatagram('127.0.0.1', 0);
$socket->close();
// `receive` on a closed socket throws.
try {
$socket->receive(64, 1s);
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
// A second `close` also throws.
try {
$socket->close();
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
Output
Core\Net\Datagram::receive: this handle is closed
Core\Net\Datagram::close: this handle is closed

Closing in finally

<?nvs
// Shows a method that always closes its socket, even when no answer arrives.
// `askStatus` asks a server for its status and waits a short time for the answer.
// The `finally` block runs in both cases, so the socket is always closed.
class Status
{
// The answer comes from the network, so the method returns a `tainted string`.
public static function askStatus(uint $serverPort): tainted string
{
Core\Net\Datagram $socket = Core\Net::bindDatagram('127.0.0.1', 0);
try {
$socket->send('127.0.0.1', $serverPort, 'status' as bytes, 1s);
return $socket->receive(64, 100ms)->payload() as string;
} catch (TimeoutError $late) {
return 'no answer';
} finally {
$socket->close();
echo "socket closed\n";
}
}
}
// This server never answers, so `askStatus` returns `no answer`.
Core\Net\Datagram $silent = Core\Net::bindDatagram('127.0.0.1', 0);
echo Status::askStatus($silent->port()), "\n";
$silent->close();
Output
socket closed
no answer