Example 1 — Local and Remote Names on a Pair
Create a socket pair and print local and remote names from one end.
<?php
$pair = stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
[$a, $b] = $pair;
$local = stream_socket_get_name($a, false);
$remote = stream_socket_get_name($a, true);
echo 'local has colon: ', str_contains($local, ':') ? 'yes' : 'no', PHP_EOL;
echo 'remote has colon: ', str_contains($remote, ':') ? 'yes' : 'no', PHP_EOL;
echo 'names differ: ', $local !== $remote ? 'yes' : 'no', PHP_EOL;
fclose($a);
fclose($b); How It Works
- stream_socket_pair() creates two connected sockets offline.
- false asks for the local address of $a.
- true asks for the remote address seen from $a.
- Exact ports vary between runs, so the demo checks shape instead of hard-coding them.

