Skip to content

Core\Net::connect

Opens a TCP connection to a server, so your program can send and receive bytes over it.

You give the host, the port and the longest time the connection may take to open. The result is a Core\Net\Stream. You call write and read on it, and close when you are done. If you do not close it, it closes when the request ends.

Your program may connect 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 before it is looked up. A server that is not running throws an IOError. A server that does not answer in time throws a TimeoutError.

Good to know: addresses inside your own network, such as 10.0.0.5, need a second setting, internal. It takes addresses and ranges such as 10.0.0.0/8. 127.0.0.1 is allowed from the command line and in development mode. A server in production needs it in internal too.

Parameters

  • $hoststringA host name or an IP address. It is checked against net.connect before its address is looked up. The connection goes to the one address that is found.
  • $portuintThe port to connect to, 1 to 65535.
  • $withinCore\Time\DurationHow long opening the connection may take. This limit ends when the connection is open. A later read uses the time limit that you give to that read.

Return value

Returns Core\Net\Stream. An open Core\Net\Stream. If your program does not close it, it is closed when the request ends.

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, or $within is not a positive length of time.
  • TimeoutErrorThe connection was not open yet when the time in $within ended.
  • IOErrorThe connection failed. The other side refused it, it could not be reached, or it was reset while it opened.

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

Sending a line to a server

<?nvs
// Shows how to open a TCP connection to a server and send it one line.
// A small server on this computer, so the example needs no network.
// Port `0` lets the operating system pick a free port.
Core\Net\Listener $server = Core\Net::listen('127.0.0.1', 0);
// `Core\Net::connect` opens the connection and returns a `Core\Net\Stream`.
// The last argument is how long the connection may take to open.
Core\Net\Stream $client = Core\Net::connect('127.0.0.1', $server->port(), 5s);
$client->write("PING\n" as bytes, 5s);
// The server side reads what arrived. This prints `PING`.
Core\Net\Stream $incoming = $server->accept(5s);
echo Core\Str::trim($incoming->read(64, 5s) as string), "\n";
$client->close();
$incoming->close();
$server->close();
Output
PING

A host that is not allowed

<?nvs
// Shows what happens when a program connects to a host its configuration does not allow.
// The `nvs.toml` beside this example allows only `127.0.0.1`.
// So this call throws a `RuntimeError`, and nothing is sent.
try {
Core\Net::connect('mail.example.com', 25, 5s);
} catch (RuntimeError $refused) {
echo $refused->message, "\n";
}
Output
Core\Net::connect needs the capability `net.connect` for mail.example.com, which is not granted
help: add `connect = ["mail.example.com"]` under `[capabilities.net]` in nvs.toml

A health check for two services

<?nvs
// Shows a health check that tries to connect to each service and reports which ones are down.
class Health
{
// A connection that opens means the service is up.
// A stopped service throws an `IOError`, and a slow one throws a `TimeoutError`.
public static function report(string $name, uint $port): void
{
try {
Core\Net\Stream $probe = Core\Net::connect('127.0.0.1', $port, 5s);
$probe->close();
echo $name, ": up\n";
} catch (IOError $down) {
echo $name, ": down\n";
} catch (TimeoutError $slow) {
echo $name, ": too slow\n";
}
}
}
// Two services on this computer. The cache is running.
// The queue is stopped, so nothing listens on its port.
Core\Net\Listener $cache = Core\Net::listen('127.0.0.1', 0);
Core\Net\Listener $queue = Core\Net::listen('127.0.0.1', 0);
uint $queuePort = $queue->port();
$queue->close();
// This prints `cache: up` and `queue: down`.
Health::report('cache', $cache->port());
Health::report('queue', $queuePort);
$cache->close();
Output
cache: up
queue: down