Skip to content

Core\Net\Stream::close

$stream->close(): void

Closes a TCP connection.

The other side can still read the bytes you wrote before close. After those bytes, its read returns no bytes, which tells it that you closed the connection.

After close, read and write throw a RuntimeError, and so does a second close. Closing a connection does not close the server that accepted it.

If your program does not call close, the connection is closed when the request ends. Call it yourself when you are done, so the other side knows 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 a server on this computer and connect to it.
# These lines allow a server only at `127.0.0.1`, on a port the system picks.
# A program run with `nvs run` may connect to this computer without a grant.
[capabilities.net]
listen = ["127.0.0.1:0"]

The other side sees the end

<?nvs
// Shows that when one side closes a connection, the other side reads the end of the data.
Core\Net\Listener $server = Core\Net::listen('127.0.0.1', 0);
Core\Net\Stream $client = Core\Net::connect('127.0.0.1', $server->port(), 5s);
Core\Net\Stream $connection = $server->accept(5s);
// The client sends one line and closes its side of the connection.
$client->write("last line\n" as bytes, 5s);
$client->close();
// The server still reads what was sent before the close. This prints `got: last line`.
echo 'got: ', Core\Str::trim($connection->read(64, 5s) as string), "\n";
// The next `read` returns no bytes, because the client closed. This prints `bytes after close: 0`.
echo 'bytes after close: ', Core\Bytes::length($connection->read(64, 5s)), "\n";
$connection->close();
$server->close();
Output
got: last line
bytes after close: 0

Using a closed connection

<?nvs
// Shows that a closed connection throws a `RuntimeError` when you use it again.
Core\Net\Listener $server = Core\Net::listen('127.0.0.1', 0);
Core\Net\Stream $client = Core\Net::connect('127.0.0.1', $server->port(), 5s);
$client->close();
// `write` on a closed connection throws.
try {
$client->write('hello' as bytes, 1s);
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
// `read` on a closed connection throws.
try {
$client->read(64, 1s);
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
// A second `close` also throws.
try {
$client->close();
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
$server->close();
Output
Core\Net\Stream::write: this handle is closed
Core\Net\Stream::read: this handle is closed
Core\Net\Stream::close: this handle is closed

Closing in finally

<?nvs
// Shows a client method that always closes its connection, even when the server does not answer.
class Health
{
// `check` sends `PING` and waits a short time for the answer.
// The answer comes from the network, so the method returns a `tainted string`.
public static function check(uint $port): tainted string
{
Core\Net\Stream $connection = Core\Net::connect('127.0.0.1', $port, 5s);
try {
$connection->write("PING\n" as bytes, 5s);
return 'answer: ' . Core\Str::trim($connection->read(64, 200ms) as string);
} catch (TimeoutError $late) {
return 'no answer';
} finally {
// The `finally` block runs in both cases, so the connection is always closed.
$connection->close();
echo "connection closed\n";
}
}
}
// This server never answers, so `check` prints `connection closed` and returns `no answer`.
Core\Net\Listener $silent = Core\Net::listen('127.0.0.1', 0);
echo Health::check($silent->port()), "\n";
$silent->close();
Output
connection closed
no answer