note 94146 deleted from function.socket-read by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 11:17:54 +0000 |
| Subject: | note 94146 deleted from function.socket-read by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197088@lists.php.net to get a copy of this message | ||
Note Submitter: philthathril at NOSPAM dot gmail dot com
----
I discovered that socket_read() was not always grabbing the length that I requested. If I requested
1024 bytes, it may return any number smaller than that. Well, due to the nature of network traffic,
there's no way to determine how much data will be read from the socket each time.
To read data of a certain length, this little method works well.
<?php
public function readSocketForDataLength ($socket, $len)
{
$offset = 0;
$socketData = '';
while ($offset < $len) {
if (($data = @socket_read ($socket, $len-$offset, $this->readType)) === false) {
$this->error();
return false;
}
$dataLen = strlen ($data);
$offset += $dataLen;
$socketData .= $data;
if ($dataLen == 0) { break; }
}
return $socketData;
}
?>
Happy coding!