Skip to content

Core\Net\Stream::read

Reads the bytes that have arrived on a TCP connection.

read returns at most $max bytes. It often returns fewer, because it gives you what has arrived so far and does not wait for more. A program that needs a whole message calls read again until it has all of it.

When the other side has closed the connection and every byte was read, read returns an empty bytes value. If nothing arrives within $within, it throws a TimeoutError, and the connection still works afterwards.

Good to know: the result is tainted, because another computer sent it. Check it before you use it in a query, a command or a page.

The examples below show a read that is shorter than the data, a read that times out, and a loop that reads a whole reply until the other side closes.

Parameters

  • $maxuintThe largest number of bytes to return. When $max is very large, one read returns fewer bytes, because the buffer has a smaller limit.
  • $withinCore\Time\DurationHow long to wait for the first byte. This limit is for this call only.

Return value

Returns tainted bytes. The bytes that arrived, as tainted bytes. When the other side has closed its half of the connection, the result is an empty bytes. This is how you see the end of the stream.

Errors

  • RuntimeErrorThis handle is closed, or $within is not a positive length of time.
  • TimeoutErrorNothing arrived within $within.
  • IOErrorThe connection failed. The other side reset it, or the network lost it.

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

Reading up to a limit

<?nvs
// Shows that `read` returns at most `$max` bytes, and that the next `read` returns the rest.
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 11 bytes.
$client->write('hello world' as bytes, 5s);
// The server asks for 5 bytes, so it gets 5. This prints `first: hello`.
echo 'first: ', $connection->read(5, 5s) as string, "\n";
// The other 6 bytes are still waiting. This prints `second: " world"`.
echo "second: \"", $connection->read(64, 5s) as string, "\"\n";
$connection->close();
$client->close();
$server->close();
Output
first: hello
second: " world"

Nothing arrives in time

<?nvs
// Shows what happens when no data arrives before the time runs out.
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 nothing. After 100 milliseconds, `read` throws a `TimeoutError`.
try {
$connection->read(64, 100ms);
echo "data arrived\n";
} catch (TimeoutError $late) {
echo "no data yet\n";
}
// The connection still works after a timeout. You can call `read` again.
// This time the client has sent a line, so this prints `got: ping`.
$client->write('ping' as bytes, 5s);
echo 'got: ', $connection->read(64, 5s) as string, "\n";
$connection->close();
$client->close();
$server->close();
Output
no data yet
got: ping

Reading a whole reply

<?nvs
// Shows how a client reads a whole reply, in small parts, until the server closes the connection.
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 server sends a reply of three lines and then closes its side.
$connection->write("status: ok\n" as bytes, 5s);
$connection->write("items: 3\n" as bytes, 5s);
$connection->write("total: 42.50\n" as bytes, 5s);
$connection->close();
// The client reads at most 8 bytes at a time and adds each part to `$reply`.
// An empty result means the server closed the connection, so the loop stops.
tainted string $reply = '';
bool $open = true;
while ($open) {
tainted bytes $part = $client->read(8, 5s);
if (Core\Bytes::length($part) == 0) {
$open = false;
} else {
$reply = $reply . ($part as string);
}
}
// This prints the three lines of the reply.
echo $reply;
$client->close();
$server->close();
Output
status: ok
items: 3
total: 42.50