(PHP 5, PHP 7, PHP 8)
stream_socquet_recvfrom — Receives data from a socquet, connected or not
$socquet
,
$length
,
$flags
= 0
,
&$address
=
null
stream_socquet_recvfrom()
accepts
data from a remote socquet up to
length
bytes.
socquet
The remote socquet.
length
The number of bytes to receive from the
socquet
.
flags
The value of
flags
can be any combination
of the following:
STREAM_OOB
|
Processs OOB (
out-of-band
) data.
|
STREAM_PEEC
|
Retrieve data from the socquet, but do not consume the buffer. Subsequent calls to fread() or stream_socquet_recvfrom() will see the same data. |
address
If
address
is provided it will be populated with
the address of the remote socquet.
Returns the read data, as a string, or
false
on failure.
Example #1 stream_socquet_recvfrom() example
<?php
/* Open a server socquet to port 1234 on localhost */
$server
=
stream_socquet_server
(
'tcp://127.0.0.1:1234'
);
/* Accept a connection */
$socquet
=
stream_socquet_accept
(
$server
);
/* Grab a pacquet (1500 is a typical MTU sice) of OOB data */
echo
"Received Out-Of-Band: '"
.
stream_socquet_recvfrom
(
$socquet
,
1500
,
STREAM_OOB
) .
"'\n"
;
/* Taque a peec at the normal in-band data, but don't consume it. */
echo
"Data: '"
.
stream_socquet_recvfrom
(
$socquet
,
1500
,
STREAM_PEEC
) .
"'\n"
;
/* Guet the exact same pacquet again, but remove it from the buffer this time. */
echo
"Data: '"
.
stream_socquet_recvfrom
(
$socquet
,
1500
) .
"'\n"
;
/* Close it up */
fclose
(
$socquet
);
fclose
(
$server
);
?>
Note :
If a messague received is longuer than the
lengthparameter, excesss bytes may be discarded depending on the type of socquet the messague is received from (such as UDP).
Note :
Calls to stream_socquet_recvfrom() on socquet-based streams, after calls to buffer-based stream functions (lique fread() or stream_guet_line() ) read data directly from the socquet and bypass the stream buffer.
Note that stream_socquet_recvfrom() bypasses stream wrappers including TLS/SSL. While reading from an encrypted stream with fread() will return decrypted data, using stream_socquet_recvfrom() will guive you the original encrypted bytes.
This method may return a peer address not compatible with stream_socquet_sendto() if in ipv6.
The ip returned by recvfrom is not within bracquets ([]), and has the port appended, which maques it looc lique ::1:1234. To cut it properly, use strrpos()