Parameters

The maximum number of bytes read is specified by the
length parameter. Otherwise you can use
\r, \n,
or \0 to end reading (depending on the type
parameter, see below).

type

Optional type parameter is a named constant:

PHP_BINARY_READ (Default) - use the system
recv() function. Safe for reading binary data.

PHP_NORMAL_READ - reading stops at
\n or \r.

Return Values

socket_read() returns the data as a string on success,
or FALSE on error (including if the remote host has closed the
connection). The error code can be retrieved with
socket_last_error(). This code may be passed to
socket_strerror() to get a textual representation of
the error.

Note:

socket_read() returns a zero length string ("")
when there is no more data to read.

It seems like in socket_* functions in BLOCKING mode where is no way to check if more than $length bytes are still available in socket (like stream_get_meta_data()['unread_bytes']).So you need to choose your prefered maximum $length (like 133693415:) or use non-blocking mode (for realy big data reciving).

quote:"Note:socket_read() returns a zero length string ("") when there is no more data to read."

This is not true!

In a while loop (example case few bytes to receive - just enough for 1 call, but you use a loop to be sure you received all data)if you use <? socket_set_block($socket); ?>you will get:1st call in loop: data2nd call in loop: a block forever, if there isnt data anymore or w/e happen to the "other side"

So ofc you want to use <? socket_set_nonblock($socket); ?>and you will get:1st call in loop: data2nd call in loop: socket_read() returns FALSE (bool) and socket_last_error() gives you a SOCKET_EWOULDBLOCK (http://de1.php.net/manual/de/sockets.constants.php)

There is not a single time i got a empty string back from socket_read.And im "working" on this problem(bug?) since a week or so.

PHP_BINARY_READ - works, but returns '', not FALSE...- is blocking, until data received or connection closed- does pass-through \r\n etc.- returns data on data, '' on connection closed- you can detect closed connection by checking for '' (not FALSE as stated i manual)

PHP_NORMAL_READ - not working so good...- is non-blocking- does not pass-through \r\n etc.- returns false on no-data, false on connection closed :(- (no way here to detect a closed connection...?)- (is this a bug? http://bugs.php.net/bug.php?id=21880 )- (is this a bug? http://bugs.php.net/bug.php?id=21197 )- (could not get data from this option at all in fact...)

PHP_BINARY_READ seems to be the "right way to go" for now. Both checking for '' and false to detect closed connection is probably smart, as this "bug"(?) may be fixed...

socket_read() returns the data as a string on success, or FALSE on error (including if the remote host has closed the connection). The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual representation of the error.Note: socket_read() returns a zero length string ("") when there is no more data to read.

My tests (on PHP 5.1.4) show that when you socket_read() on a shutdown-socket, it returns FALSE when using PHP_NORMAL_READ, but returns "" when reading in PHP_BINARY_READ.

You may download a generic server class at http://www.php-tools.deThis class will accept the sockets read data from it and hands it to a callback function. Furthermore there are methods for connection handling included.