Voting

: min(nine, seven)?
(Example: nine)

The Note You're Voting On

d dot bergloev at gmail dot com
5 years ago
Binary read is the correct way to read data in most cases, whereas "normal read", is a strange and lazy PHP built mode, that works mostly with terminal data.

If you want to keep track of closed connections with binary read, the correct way is NOT to switch from binary to "normal", like some suggests. The correct way is to create some test scenarios and see how PHP deals with specific circumstances.

Here is what a quick test shows, when working with non-blocking sockets.

<?php
$input
= socket_read($socket, 1024);

// In most cases, error produces an empty string and not FALSE
if ($input === FALSE || strcmp($input, '') == 0) {
$code = socket_last_error($socket);

// You MUST clear the error, or it will not change on next read
socket_clear_error($socket);

if (
$code == SOCKET_EAGAIN) {
// Nothing to read from non-blocking socket, try again later...

} else {
// Connection most likely closed, especially if $code is '0'
}

} else {
// Deal with the data
}
?>

There are more errors to consider, but this will get you started.

<< Back to user notes page

To Top