socket_getpeername
Description
The socket_getpeername of Sockets for PHP queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type.
Syntax
socket_getpeername( 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_getpeername() will return the peers (remote) 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_getpeername() will return the Unix filesystem path (e.g. /tmp/mysocket) in the address parameter.
port
If given, this will hold the port associated to address.
Return
Returns true on success or false on failure.
socket_getpeername() 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 · 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"); } $address = '127.0.0.1'; $port = 80; $socket_connect = socket_connect($socket, $address, $port); if($socket_connect === false) { $error_code = socket_last_error($socket); $socket_strerror = socket_strerror($error_code); die("socket_connect: $socket_strerror"); } $return = socket_getpeername($socket, $address, $port); var_export($return); socket_close($socket);
true
Links
Related
Sockets
- socket_accept
- socket_addrinfo_bind
- socket_addrinfo_connect
- socket_addrinfo_explain
- socket_addrinfo_lookup
- socket_atmark
- socket_bind
- socket_clear_error
- socket_close
- socket_cmsg_space
- socket_connect
- socket_create
- socket_create_listen
- socket_create_pair
- socket_export_stream
- socket_get_option
- socket_getopt
- socket_getsockname
- socket_import_stream
- socket_last_error
- socket_listen
- socket_read
- socket_recv
- socket_recvfrom
- socket_recvmsg
- socket_select
- socket_send
- socket_sendmsg
- socket_sendto
- socket_set_block
- socket_set_nonblock
- socket_set_option
- socket_setopt
- socket_shutdown
- socket_strerror
- socket_write
- socket_wsaprotocol_info_export
- socket_wsaprotocol_info_import
- socket_wsaprotocol_info_release