Content · Web Development · PHP

PHP socket_getpeername() Function: Usage and Examples

Gets the network address and optional port of the peer connected to a socket.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Gets the network address and optional port of the peer connected to a socket.

Before using it, identify which data, connection or runtime state the operation reads or changes. This avoids examples that appear to work but apply to a different context.

What Is socket_getpeername() in PHP?


Gets the network address and optional port of the peer connected to a socket.

Syntax:

<?php

// Signature for reference only
socket_getpeername(Socket $socket, $address, [optional] $port) : bool

Parameters and Return Value


  • $socket — Required Socket argument supplied to this operation.
  • $address — Required value argument supplied to this operation.
  • $port — Optional value argument supplied to this operation.

The declared return type is bool.

Example 1: Basic Usage


<?php

$socketHandle = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socketHandle === false) { throw new RuntimeException("Could not create socket"); }
var_dump(socket_getpeername($socketHandle, $remoteAddress));
socket_close($socketHandle);

Gets the network address and optional port of the peer connected to a socket.

Example 2: Another Case


<?php

$socketHandle = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($socketHandle === false) { throw new RuntimeException("Could not create socket"); }
var_dump(socket_getpeername($socketHandle, $remoteAddress, $remotePort));
socket_close($socketHandle);

The second sample illustrates a different way to inspect or handle this operation. Configure the underlying session, socket, key material or connection first.

Practical Example


<?php

$socketHandle = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socketHandle === false) { throw new RuntimeException("Socket unavailable"); }
$success = socket_getpeername($socketHandle, $remoteAddress, $remotePort);
if ($success) { echo $remoteAddress . ":" . $remotePort; }
socket_close($socketHandle);

The practical example is a teaching scenario. Change sample configuration and credentials before using a similar design in an application.

Important Behavior and Edge Cases


  • A live socket and correct address family are required; a disconnected endpoint cannot return peer information.
  • Relevant PHP environment: sockets. Ensure the required extension and operating-system capabilities are present.
  • Check results and release resources where necessary; successful syntax alone does not guarantee a successful operation.

Comparison With Related Functions


OptionWhen to Use It
socket_getpeernameGets the network address and optional port of the peer connected to a socket.
socket_get_optionUse this related operation for its different responsibilities; do not assume identical inputs or return values.

Common Beginner Mistakes


MistakeCorrect Approach
Assuming a successful return valueInspect the returned value and handle failures according to this operation’s contract.
Using the API without its required environmentSupply the required resources, runtime configuration and permissions before calling it.
Copying demonstration values into productionUse application-specific validated values, secure configuration and safe error handling.

Key Takeaways


  • Gets the network address and optional port of the peer connected to a socket.
  • A live socket and correct address family are required; a disconnected endpoint cannot return peer information.
  • Validate inputs and handle failure conditions before integrating this operation into a production workflow.

Gets the network address and optional port of the peer connected to a socket. Use the appropriate runtime and treat any environment-dependent output as an example, not a guaranteed result.

Share this resourceSend this page to someone who may find it useful.