Skip to content

Core\Net\Datagram\Message::port

$message->port(): uint

Returns the port of the program that sent a message. Together with host(), it is the address where a reply goes. The result is always between 1 and 65535.

Every open socket has its own port. So when two programs on the same computer send to you, the host is the same for both, and the port tells them apart.

The port is the port of the sender's socket, not of your own socket. Your own port is Core\Net\Datagram::port.

Return value

Returns uint. The sender's port, 1 to 65535.

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 senders port

<?nvs
// Shows how to find out which port a UDP message was sent 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);
// `port()` returns the port of the socket that sent the message.
// The number is different on every run, so this prints `from the client: yes`.
Core\Net\Datagram\Message $message = $server->receive(64, 5s);
echo 'from the client: ', $message->port() == $client->port() ? 'yes' : 'no', "\n";
$client->close();
$server->close();
Output
from the client: yes

Telling two senders apart

<?nvs
// Shows how the port tells apart two senders that are on the same computer.
Core\Net\Datagram $server = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $first = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $second = Core\Net::bindDatagram('127.0.0.1', 0);
$first->send('127.0.0.1', $server->port(), 'a' as bytes, 1s);
$second->send('127.0.0.1', $server->port(), 'b' as bytes, 1s);
Core\Net\Datagram\Message $one = $server->receive(64, 5s);
Core\Net\Datagram\Message $two = $server->receive(64, 5s);
// Both messages have the host `127.0.0.1`. This prints `same host: yes`.
echo 'same host: ', $one->host() == $two->host() ? 'yes' : 'no', "\n";
// The ports are different. This prints `different ports: yes`.
echo 'different ports: ', $one->port() != $two->port() ? 'yes' : 'no', "\n";
$first->close();
$second->close();
$server->close();
Output
same host: yes
different ports: yes

A game server that numbers its players

<?nvs
// Shows a game server that gives each player a number and tells each player its own number.
Core\Net\Datagram $server = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $alice = Core\Net::bindDatagram('127.0.0.1', 0);
Core\Net\Datagram $bob = Core\Net::bindDatagram('127.0.0.1', 0);
$alice->send('127.0.0.1', $server->port(), 'join' as bytes, 1s);
$bob->send('127.0.0.1', $server->port(), 'join' as bytes, 1s);
// The server answers each `join` at the host and port it came from.
uint $player = 1;
while ($player <= 2) {
Core\Net\Datagram\Message $join = $server->receive(64, 5s);
string $reply = 'you are player ' . $player;
$server->send($join->host(), $join->port(), $reply as bytes, 1s);
$player = $player + 1;
}
// Each player gets only its own reply. This prints `alice: you are player 1` and `bob: you are player 2`.
echo 'alice: ', $alice->receive(64, 5s)->payload() as string, "\n";
echo 'bob: ', $bob->receive(64, 5s)->payload() as string, "\n";
$alice->close();
$bob->close();
$server->close();
Output
alice: you are player 1
bob: you are player 2