PHP stream_socket_get_name() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_socket_get_name() returns the local or remote address name of a socket stream.

Pass false for the local side and true for the remote peer. The string usually looks like 127.0.0.1:port. It returns false when the name cannot be read. Works well with stream_socket_pair() for offline demos.

Syntax

Call stream_socket_get_name() with the values you need. Start with this line.

PHP
$remote = stream_socket_get_name($socket, true);
CallWhat it does
stream_socket_get_name($socket, false)Returns the local address string.
stream_socket_get_name($socket, true)Returns the remote peer address string.
stream_socket_get_name($client, true)Shows the client peer after accept or connect.

true means remote, false means local. On a stream_socket_pair(), each end can report names for both sides. Useful for logging which client connected to your server.

Library declarations
PHP
stream_socket_get_name(resource $socket, bool $remote): string|false

Parameters

ParameterDescription
socketRequired. A socket stream from client, server, accept, or pair.
remoteRequired. true for the remote name, false for the local name.

Return Value

Return ValueDescription
stringAn address string such as 127.0.0.1:54321.
falseThe name could not be retrieved.

PHP Examples

Example 1 — Local and Remote Names on a Pair

Create a socket pair and print local and remote names from one end.

PHP
<?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

  1. stream_socket_pair() creates two connected sockets offline.
  2. false asks for the local address of $a.
  3. true asks for the remote address seen from $a.
  4. Exact ports vary between runs, so the demo checks shape instead of hard-coding them.

Example 2 — Names From Both Ends

Compare remote names reported by each end of the pair.

PHP
<?php
$pair = stream_socket_pair(STREAM_PF_INET, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
[$a, $b] = $pair;

$aRemote = stream_socket_get_name($a, true);
$bRemote = stream_socket_get_name($b, true);
echo 'both remotes set: ', ($aRemote && $bRemote) ? 'yes' : 'no', PHP_EOL;
echo 'remotes differ: ', $aRemote !== $bRemote ? 'yes' : 'no', PHP_EOL;

fclose($a);
fclose($b);

How It Works

  1. Each end sees the other as remote.
  2. Both remote names are non-empty strings.
  3. The two remote names differ because each peer looks at the other end.
  4. Close both streams when finished.

Example 3 — Illustrative Server Peer Log

Show how a server would log a peer after accept, using fixed sample text.

PHP
<?php
// After $client = stream_socket_accept($server, -1, $peer);
// or $peer = stream_socket_get_name($client, true);
$peer = '127.0.0.1:54321';
echo 'client connected from ', $peer, PHP_EOL;

How It Works

  1. Accept returns a client stream on a live server.
  2. stream_socket_get_name($client, true) reads the peer.
  3. Logging the peer helps debug connection issues.
  4. A listening server is required for a real accept.

Frequently Asked Questions

It returns the local or remote socket address as a string, or false on failure.
true returns the remote peer name. false returns the local name of the socket.
Yes. stream_socket_pair() provides connected sockets that work offline.
Often host:port for TCP, such as 127.0.0.1:8080. Formats differ for IPv6 or Unix sockets.
When the stream is not a socket or the name cannot be read.

Next: stream_socket_pair()

stream_socket_pair() creates a pair of connected, indistinguishable socket streams.

stream_socket_pair() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful