Skip to content

Core\Net\Stream::write

Sends bytes to the other side of a TCP connection.

write returns how many bytes it sent. This can be fewer than you gave it, when the connection cannot take more right now. To send a whole message, call write again with the bytes that are left, until nothing is left.

If the connection cannot take any bytes within $within, write throws a TimeoutError. This happens when the other side stops reading. If the other side has closed the connection, write throws an IOError.

The examples below show the count that write returns, a write that times out, and a function that sends a whole message.

Parameters

  • $payloadbytesThe bytes to send.
  • $withinCore\Time\DurationHow long to wait until the socket accepts bytes. This limit is for this call only.

Return value

Returns uint. How many bytes of $payload were written. It is between 0 and the length of $payload.

Errors

  • RuntimeErrorThis handle is closed, or $within is not a positive length of time.
  • TimeoutErrorThe socket accepted nothing within $within.
  • IOErrorThe connection failed. The other side reset it, or closed its half for reading.

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

Counting what was sent

<?nvs
// Shows that `write` sends bytes to the other side and returns how many it sent.
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 5 bytes. This prints `sent: 5`.
echo 'sent: ', $client->write('hello' as bytes, 5s), "\n";
// The server reads them. This prints `got: hello`.
echo 'got: ', $connection->read(64, 5s) as string, "\n";
// An empty `bytes` value sends nothing. This prints `sent: 0`.
echo 'sent: ', $client->write('' as bytes, 5s), "\n";
$connection->close();
$client->close();
$server->close();
Output
sent: 5
got: hello
sent: 0

The other side is not reading

<?nvs
// Shows what happens when the other side stops reading and `write` cannot send more in time.
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 never reads, so the connection fills up.
// When `write` cannot send anything for 100 milliseconds, it throws a `TimeoutError`.
bytes $block = Core\Str::repeat('x', 65536) as bytes;
bool $sending = true;
uint $rounds = 0;
while ($sending && $rounds < 100000) {
try {
$client->write($block, 100ms);
} catch (TimeoutError $late) {
$sending = false;
}
$rounds = $rounds + 1;
}
// This prints `the server is not reading`.
echo $sending ? 'everything was sent' : 'the server is not reading', "\n";
$connection->close();
$client->close();
$server->close();
Output
the server is not reading

Sending a whole message

<?nvs
// Shows a function that sends a whole message, because one `write` may send only part of it.
class Sender
{
// `write` returns how many bytes it sent. The loop sends the rest until nothing is left.
public static function sendAll(Core\Net\Stream $stream, bytes $message): void
{
uint $sent = 0;
while ($sent < Core\Bytes::length($message)) {
$sent = $sent + $stream->write(Core\Bytes::slice($message, $sent as int), 5s);
}
}
}
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 a request of three lines and then closes its side.
Sender::sendAll($client, "GET /orders/42 HTTP/1.1\r\nHost: shop.example.com\r\n\r\n" as bytes);
$client->close();
// The server reads until the client has closed. This prints `request: 51 bytes`.
uint $size = 0;
bool $open = true;
while ($open) {
uint $got = Core\Bytes::length($connection->read(16, 5s));
$size = $size + $got;
$open = $got > 0;
}
echo 'request: ', $size, " bytes\n";
$connection->close();
$server->close();
Output
request: 51 bytes