Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

socket_getsockname

Description

The socket_getsockname of Sockets for PHP queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type.

Syntax

socket_getsockname(
    Socket $socket,
    string &$address,
    int &$port = null
): bool

Parameters

socket

A Socket instance created with socket_create() or socket_accept().

address

If the given socket is of type AF_INET or AF_INET6, socket_getsockname() will return the local IP address in appropriate notation (e.g. 127.0.0.1 or fe80::1) in the address parameter and, if the optional port parameter is present, also the associated port.

If the given socket is of type AF_UNIX, socket_getsockname() will return the Unix filesystem path (e.g. /tmp/mysocket) in the address parameter.

port

If provided, this will hold the associated port.

Return

Returns true on success or false on failure.

socket_getsockname() may also return false if the socket type is not any of AF_INET, AF_INET6, or AF_UNIX, in which case the last socket error code is not updated.

Examples

1 · socket address · AF_INET

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$socket = socket_create($domain, $type, $protocol);

    if($socket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $return = socket_getsockname($socket, $address);

    var_export($return);

socket_close($socket);

?>
true

2 · socket address · AF_UNIX

<?

$domain = AF_UNIX;
$type = SOCK_STREAM;
$protocol = 0;

$socket = socket_create($domain, $type, $protocol);

    if($socket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $return = socket_getsockname($socket, $address);

    var_export($return);

socket_close($socket);

?>
true

3 · port

<?

$domain = AF_INET;
$type = SOCK_STREAM;
$protocol = SOL_TCP;

$socket = socket_create($domain, $type, $protocol);

    if($socket === false)
    {
        $error_code = socket_last_error();

        $socket_strerror = socket_strerror($error_code);

        die("socket_create: $socket_strerror");
    }

    $return = socket_getsockname($socket, $address, $port);

    var_export($return);

socket_close($socket);

?>
true
HomeMenu