note 47789 added to function.socket-recv
| From: | engineat | Date: | Tue, 30 Nov 2004 12:58:28 +0000 |
| Subject: | note 47789 added to function.socket-recv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-81178@lists.php.net to get a copy of this message | ||
To read from socket both on linux and windows OS having flash as a client I use function bellow.
$length is the size of a chunk, not the max length to read. It will continue reading until EOL char
occures or client disconnects (or in case of error), so it works for bigger packets as well.
function read($descriptor, $length = 1024) {
$this->method = "read";
if(!$client){
echo("No valid socket descriptor !\n");
return false;
}
$read ='';
while(($flag=socket_recv($descriptor, $buf, $length,0))>0){
$asc=ord(substr($buf, -1));
if ($asc==0) {
$read.=substr($buf,0,-1);
break;
}else{
$read.=$buf;
}
}
if ($flag<0){
//error
return false;
}elseif ($flag==0){
//Client disconnected
return false;
}else{
return $read;
}
}
----
Manual Page -- http://www.php.net/manual/en/function.socket-recv.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47789
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47789&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47789&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47789&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47789&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47789&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47789&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47789&report=yes
Search -- http://master.php.net/manage/user-notes.php