Skip to content

Core\Net\Datagram\Message::host

$message->host(): string

Returns the address of the program that sent a message, as text such as 127.0.0.1 or ::1. It is always an address and never a host name.

The result is a plain string, so you can give it straight to Core\Net\Datagram::send to reply. send still checks the address against the connect list in the [capabilities.net] block of nvs.toml, the same as any other address. A reply to an address that is not in that list throws a RuntimeError.

Anybody on the network can send a message to your socket. Compare the address with a list of addresses you trust before you act on a message.

Return value

Returns string. An IP address, never a host name, because the address is not looked up in reverse.

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

Who sent it

<?nvs
// Shows how to find out which address a UDP message came from.
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(), 'hello' as bytes, 1s);
// `host()` returns the address of the sender as text. This prints `from: 127.0.0.1`.
Core\Net\Datagram\Message $message = $server->receive(64, 5s);
echo 'from: ', $message->host(), "\n";
$client->close();
$server->close();
Output
from: 127.0.0.1

Replying to the sender

<?nvs
// Shows how to send a reply to the program that sent a message.
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(), 'ping' as bytes, 1s);
// `host()` and `port()` together are the address of the sender. The reply goes there.
Core\Net\Datagram\Message $request = $server->receive(64, 5s);
$server->send($request->host(), $request->port(), 'pong' as bytes, 1s);
// This prints `client got: pong`.
echo 'client got: ', $client->receive(64, 5s)->payload() as string, "\n";
$client->close();
$server->close();
Output
client got: pong

Accepting only known senders

<?nvs
// Shows a server that acts only on messages from addresses it trusts.
// This server trusts two machines of its own network. Every other sender is ignored.
array<string> $trusted = ['10.0.0.7', '10.0.0.8'];
Core\Net\Datagram $server = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $stranger = Core\Net::bindDatagram('127.0.0.1', 0);
$stranger->send('127.0.0.1', $server->port(), 'restart' as bytes, 1s);
Core\Net\Datagram\Message $message = $server->receive(64, 5s);
bool $known = false;
foreach ($trusted as string $address) {
if ($message->host() == $address) {
$known = true;
}
}
// `127.0.0.1` is not in the list. This prints `ignored a message from 127.0.0.1`.
if ($known) {
echo "restarting\n";
} else {
echo 'ignored a message from ', $message->host(), "\n";
}
$stranger->close();
$server->close();
Output
ignored a message from 127.0.0.1