socket_sendmsg
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
Send a message
Description
The function socket_sendmsg() sends the message described by message through the socket socket, using the sendmsg() system call.
Parameters
socketA
Socketinstance created withsocket_create(),socket_accept()orsocket_create_pair().messageAn associative array which may contain the following elements. All of them are optional, and unrecognized keys are silently ignored. In particular there is no
flagselement when sending; that key only appears in the array filled in bysocket_recvmsg().nameThe address of the remote host, as an array. Its keys are
addrandportforAF_INET;addr,port,flowinfoandscope_idforAF_INET6; andpathforAF_UNIX.addraccepts an IP address or a host name, which is then resolved. An optionalfamilykey selects the address family explicitly; if omitted, the family ofsocketis used. A family the socket does not support makes the call fail.iovA 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.controlA list of ancillary data messages. Each is an array with the keys
level,typeanddata. Only the pairs below are supported, and their availability depends on the platform.leveltypedataSOL_SOCKETSCM_RIGHTSA non-empty list of Socketinstances or stream resources, whose file descriptors are sent to the receiving process.SOL_SOCKETSCM_CREDENTIALSAn array with the keys pid,uidandgid. NamedSCM_CREDSorSCM_CREDS2on some systems.IPPROTO_IPV6IPV6_PKTINFOAn array with the keys addrandifindex.IPPROTO_IPV6IPV6_HOPLIMITAn int.IPPROTO_IPV6IPV6_TCLASSAn int.
flagsThe value of
flagscan be any combination of the following flags, joined with the binary OR (|) operator.MSG_OOBSend OOB (out-of-band) data. MSG_EORIndicate a record mark. The sent data completes the record. MSG_DONTWAITWith this flag set, the function returns even if it would normally have blocked. MSG_DONTROUTEBypass routing, use direct interface.
Return Values
Returns the number of bytes sent, Falseforfailure.
Changelog
| Version | Description |
|---|
Examples
Sending a message with socket_sendmsg()
<?php
$server = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($server, '127.0.0.1', 1053);
$client = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$sent = socket_sendmsg($client, [
'name' => ['addr' => '127.0.0.1', 'port' => 1053],
'iov' => ['Hello ', 'world'],
], 0);
echo "sent: $sent\n";
socket_recvfrom($server, $buffer, 64, 0, $from, $port);
echo "received: $buffer\n";
?>The above example will output:
sent: 11
received: Hello worldPassing 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);
?>The above example will output:
first line: <?php