Content · Web Development · PHP

PHP socket_getsockname() Function: Usage and Examples

Gets the local network address and optional port bound to a socket.

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

Gets the local network address and optional port bound 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_getsockname() in PHP?


Gets the local network address and optional port bound to a socket.

Syntax:

<?php

// Signature for reference only
socket_getsockname(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_getsockname($socketHandle, $localAddress));
socket_close($socketHandle);

Gets the local network address and optional port bound 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_getsockname($socketHandle, $localAddress, $localPort));
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_getsockname($socketHandle, $localAddress, $localPort);
if ($success) { echo $localAddress . ":" . $localPort; }
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_getsocknameGets the local network address and optional port bound 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 local network address and optional port bound 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 local network address and optional port bound 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.