Skip to content

Core\Net\Datagram::port

$datagram->port(): uint

Returns the port number a UDP socket is bound to.

You usually open a socket with port 0, which means "pick any free port". The operating system then picks the port, and port() is how your program finds out which one it is. You give that number to the program that should send messages to this socket.

The result is always between 1 and 65535. Calling port() on a socket that is already closed throws a RuntimeError.

Return value

Returns uint. The port, from 1 to 65535.

Errors

  • RuntimeErrorThis handle is closed.
  • IOErrorThe operating system did not return the port of this socket.

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

The port the system picked

<?nvs
// Shows how to find out which port the operating system picked for a socket.
// Port `0` means "pick any free port".
Core\Net\Datagram $socket = Core\Net::bindDatagram('127.0.0.1', 0);
// `port()` returns the picked port. The number is different on every run,
// so this prints `a real port: yes` and not the number.
uint $port = $socket->port();
echo 'a real port: ', $port >= 1 && $port <= 65535 ? 'yes' : 'no', "\n";
// The port stays the same while the socket is open. This prints `same: yes`.
echo 'same: ', $socket->port() == $port ? 'yes' : 'no', "\n";
$socket->close();
Output
a real port: yes
same: yes

Two sockets two ports

<?nvs
// Shows that two sockets open at the same time always get different ports.
Core\Net\Datagram $a = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $b = Core\Net::bindDatagram('127.0.0.1', 0);
// Only one open socket can use a port on an address. This prints `different: yes`.
echo 'different: ', $a->port() != $b->port() ? 'yes' : 'no', "\n";
$a->close();
$b->close();
// After `close`, the socket has no port. `port()` throws a `RuntimeError`.
try {
$a->port();
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
Output
different: yes
Core\Net\Datagram::port: this handle is closed

Telling a client where to send

<?nvs
// Shows a test that starts a UDP server on a free port and tells a client where to send.
// This class is the client. It only needs to know the port of the server.
class Client
{
public static function sendPing(uint $serverPort): void
{
Core\Net\Datagram $socket = Core\Net::bindDatagram('127.0.0.1', 0);
$socket->send('127.0.0.1', $serverPort, 'ping' as bytes, 1s);
$socket->close();
}
}
// A free port means two tests can run at the same time without a conflict.
Core\Net\Datagram $server = Core\Net::bindDatagram('127.0.0.1', 0);
Client::sendPing($server->port());
// This prints `server got: ping`.
echo 'server got: ', $server->receive(64, 5s)->payload() as string, "\n";
$server->close();
Output
server got: ping