An associative array which may contain the following elements. All of them
are optional, and unrecognized keys are silently ignored. In particular
there is no flags element when sending; that key only
appears in the array filled in by socket_recvmsg().
name
The address of the remote host, as an array. Its keys are
addr and port for
AF_INET; addr,
port, flowinfo and
scope_id for AF_INET6; and
path for AF_UNIX.
addr accepts an IP address or a host name, which is
then resolved. An optional family key selects the
address family explicitly; if omitted, the family of
socket is used. A family the socket does not
support makes the call fail.
iov
A list of buffers holding the data to send, which are sent in order as a
single message. Array keys are ignored and the values are converted to
string. When this key is missing or empty, no data is sent and the
function returns 0.
control
A list of ancillary data messages. Each is an array with the keys
level, type and
data. Only the pairs below are supported, and their
availability depends on the platform.
Example #2 Passing a file descriptor over a UNIX socket
The control key carries ancillary data. With
SCM_RIGHTS, it transfers open file descriptors to the
process at the other end of a UNIX socket. This is not available on Windows.
<?php
socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $pair);
[$sender, $receiver] = $pair;
$file = fopen(__FILE__, 'r');
socket_sendmsg($sender, [
// At least one byte of regular data must accompany the ancillary data.
'iov' => ['fd'],
'control' => [
['level' => SOL_SOCKET, 'type' => SCM_RIGHTS, 'data' => [$file]],
],
], 0);
$message = [
'buffer_size' => 16,
'controllen' => socket_cmsg_space(SOL_SOCKET, SCM_RIGHTS, 1),
];
socket_recvmsg($receiver, $message, 0);
$received = $message['control'][0]['data'][0];
echo "first line: ", fgets($received);
?>