[PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8]
socket_recv — Receives data from a connected socket
Description
socket_recv[
Socket $socket
,
?string
&$data
,
int $length
,
int $flags
]: int|false
data
is passed by reference, so it must be specified as a variable in the argument list. Data read from socket
by socket_recv[] will be returned in data
.
Parameters
socket
The socket
must be a Socket instance previously created by socket_create[].
data
The data received will be fetched to the variable specified with data
. If an error occurs, if the connection is reset, or if no data is available, data
will be set to null
.
length
Up to length
bytes will be
fetched from remote host.
flags
The value of flags
can be any combination of the following flags, joined with the binary OR [|
] operator.
flags
MSG_OOB
| Process out-of-band data. |
MSG_PEEK
| Receive data from the beginning of the receive queue without removing it from the queue. |
MSG_WAITALL
| Block until at least length are received. However, if a signal is caught or the remote host disconnects, the function may return less data.
|
MSG_DONTWAIT
| With this flag set, the function returns even if it would normally have blocked. |
Return Values
socket_recv[] returns the number of bytes received, or false
if there was an error. The actual error code can be retrieved by calling socket_last_error[]. This error code may be passed to
socket_strerror[] to get a textual explanation of the error.
Changelog
8.0.0 | socket is a Socket instance now; previously, it was a resource.
|
Examples
Example #1 socket_recv[] example
This example is a simple rewrite of the first example from Examples to use socket_recv[].
The above example will produce something like:
TCP/IP Connection
OK. Attempting to connect to '208.77.188.166' on port '80'...OK. Sending HTTP HEAD request...OK. Reading response: Read 123 bytes from socket_recv[]. Closing socket...HTTP/1.1 200 OK Date: Mon, 14 Sep 2009 08:56:36 GMT Server: Apache/2.2.3 [Red Hat] Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT ETag: "b80f4-1b6-80bfd280" Accept-Ranges: bytes Content-Length: 438 Connection: close Content-Type: text/html; charset=UTF-8 OK.
dgk at tcde dot ru ¶
17 years ago
I've used socket_select and socket_recv with a while loop and found myself in trouble when remote side closed connection. The code below produced infinite loop and socket_select returned immediately [which lead to high cpu time consumption].
The solution was simple, but quite hard to find because socket_recv is not documented. socket_recv returns FALSE if there is no data and 0 if the socket is widowed [disconnected by remote side]. So I had just to check return value of socket_recv. The problem now sounds stupid, but I've spend some time to find it out.
I hope this will save some of somebody's hair ;]
m_lajos at hotmail dot com ¶
8 years ago
Workaround for the missing MSG_DONTWAIT flag according to the bug report page:
ss-130 at yandex dot ru ¶
9 years ago
rathamahata at rathamahata dot net ¶
17 years ago
It looks like that mysterious flags are just the recv[2] flags passed to your OS syscall and nothing more...
ext/sockets/sockets.c:PHP_FUNCTION[socket_recv]
...
if [[retval = recv[php_sock->bsd_socket, recv_buf, len, flags]] < 1] {
efree[recv_buf];
...
for linux you can type `man 2 recv' and you will see complete description of thouse flags.
Sergey S. Kosrtyliov
//www.rathamahata.net/
bastiaan at [no-spam] megabass dot nl ¶
18 years ago
in case you want to empty/unset $buffer, but failing to do so, try using 0 as flag.
PHP_NORMAL_READ and PHP_BINARY_READ respectively hold 1 and 2 as value.
lexkrstn at gmail dot com ¶
3 years ago
It seems like the flags are just passed to the underlying recv[] function of your OS, hence there no MSG_DONTWAIT flag on Windows and you should not define it yourself in that case, it just won't work.
Anonymous ¶
1 year ago