note 124492 added to function.socket-read
| From: | d dot bergloev at gmail dot com | Date: | Sat, 07 Dec 2019 22:04:45 +0000 |
| Subject: | note 124492 added to function.socket-read | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-213419@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.195 (proxied: 134.90.149.147)
----
Manual Page -- http://php.net/manual/en/function.socket-read.php
Edit -- https://master.php.net/note/edit/124492
Del: integrated -- https://master.php.net/note/delete/124492/integrated
Del: useless -- https://master.php.net/note/delete/124492/useless
Del: bad code -- https://master.php.net/note/delete/124492/bad+code
Del: spam -- https://master.php.net/note/delete/124492/spam
Del: non-english -- https://master.php.net/note/delete/124492/non-english
Del: in docs -- https://master.php.net/note/delete/124492/in+docs
Del: other reasons-- https://master.php.net/note/delete/124492
Reject -- https://master.php.net/note/reject/124492
Search -- https://master.php.net/manage/user-notes.php