note 94146 added to function.socket-read
| From: | philthathril at NOSPAM dot gmail dot com | Date: | Mon, 19 Oct 2009 17:49:34 +0000 |
| Subject: | note 94146 added to function.socket-read | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161875@lists.php.net to get a copy of this message | ||
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!
----
Server IP: 192.41.40.227
Probable Submitter: 69.26.215.166
----
Manual Page -- http://www.php.net/manual/en/function.socket-read.php
Edit -- https://master.php.net/note/edit/94146
Del: integrated -- https://master.php.net/note/delete/94146/integrated
Del: useless -- https://master.php.net/note/delete/94146/useless
Del: bad code -- https://master.php.net/note/delete/94146/bad+code
Del: spam -- https://master.php.net/note/delete/94146/spam
Del: non-english -- https://master.php.net/note/delete/94146/non-english
Del: in docs -- https://master.php.net/note/delete/94146/in+docs
Del: other reasons-- https://master.php.net/note/delete/94146
Reject -- https://master.php.net/note/reject/94146
Search -- https://master.php.net/manage/user-notes.php