PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

socket_recvfrom> <socket_read
Last updated: Fri, 26 Sep 2008

view this page in

socket_recv

(PHP 4 >= 4.0.7, PHP 5)

socket_recvReceives data from a connected socket

Description

int socket_recv ( resource $socket , string &$buf , int $len , int $flags )

Warning

This function is currently not documented; only its argument list is available.



socket_recvfrom> <socket_read
Last updated: Fri, 26 Sep 2008
 
add a note add a note User Contributed Notes
socket_recv
teamcms * gmail * com
26-Jul-2007 08:00
Return value:
-----------------------------

a) on success returns number of bytes read

b) in case of no data on line, returns zero and $buf will be set to NULL.

c) on failure returns false, and $buf will be set to NULL.
To get the error code/message, call the appropriate socket functions.

d) in case of disconnect, the function returns either b) or c) which depends on how connection was closed from the other end. It returns 0 if the connection was closed gracefully with FIN squence and false if it was reset.
10-Feb-2006 02:22
On the "mysterious" int flags, I here give you a copy of the explanation from a *NIX manual:

       The flags argument to a recv() call is formed by OR'ing one or more of the following values:

       MSG_OOB
              This flag requests receipt of out-of-band data that would not be received in the normal data stream.   Some
              protocols place expedited data at the head of the normal data queue, and thus this flag cannot be used with
              such protocols.

       MSG_PEEK
              This flag causes the receive operation to return data from the  beginning  of  the  receive  queue  without
              removing that data from the queue.  Thus, a subsequent receive call will return the same data.

       MSG_WAITALL
              This  flag  requests  that  the operation block until the full request is satisfied.  However, the call may
              still return less data than requested if a signal is caught, an error or disconnect  occurs,  or  the  next
              data to be received is of a different type than that returned.

       MSG_TRUNC
              Return the real length of the packet, even when it was longer than the passed buffer. Only valid for packet
              sockets.
rathamahata at rathamahata dot net
25-Aug-2005 01:44
It looks like that mysterious flags are just the recv(2) flags passed to your OS syscall and nothing more...

ext/sockets/sockets.c:PHP_FUNCTION(socket_recv)
...
        if ((retval = recv(php_sock->bsd_socket, recv_buf, len, flags)) < 1) {
                efree(recv_buf);
...

for linux you can type `man 2 recv' and you will see complete description of thouse flags.

Sergey S. Kosrtyliov <rathamahata@rathamahata.net>
http://www.rathamahata.net/
06-May-2005 01:59
My last post was incorrect. The int flag set to 2 apparently reset the file position pointer so what I was reading was the first record repeatedly.

My workaroud ended up being the following:

for($ct=1; $ct<=$numrecs; $ct++) {
    $rec = "";
    $nr=socket_recv($fp,$rec,76,0);
       
    //grab the extra bytes.
    $terminator = "";
    while ($terminator != ".") {
        $nr=socket_recv($fp,$terminator,1,0);
    }
   
     $custarray[]=substr($rec,0,76);        
}

Martin K.
05-May-2005 04:42
I'm glad that Bastion left the above post about the mysterious int flag. He just helped to fix a problem that I've spent six hours on. Here's my code:

for($ct=1; $ct<=$numrecs; $ct++) {
     $rec = "";
     $nr=socket_recv($fp,$rec,77,0);
     print "Rec # $ct -->";
         print "$rec";
         print "<br>";
      }

The code is pretty simple, it just loops through all my records and prints them out. All records are 77 bytes and all end with a period. The first 36 records print perfectly then at 37 things go bad. The records start to get offset. The last few characters of the 37th record end up printing on the 38th record. The data on the sending side was perfect, so I knew that the problem was with socked_recv.

After reading the above post I tried changing the int flag. Changing the flag to 2 worked:
$nr=socket_recv($fp,$rec,77,2);

Now everything lines up perfectly. I had always left int flag as 0 since it's undocumented.

Martin K.
engine at [NO SPAM] illusiononly dot com
30-Nov-2004 01:58
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;
        }

     }
dgk at tcde dot ru
05-Nov-2004 12:57
I've used socket_select and socket_recv with a while loop and found myself in trouble when remote side closed connection. The code below produced infinite loop and socket_select returned immediately (which lead to high cpu time consumption).

<?

socket_set_nonblock($my_socket);
$streams = array($my_socket/*, ... */);

$lastAccess = time();
while (socket_select($streams, $write = NULL, $except = NULL, SLEEP_TIME_SECONDS, SLEEP_TIME_MILLISECONDS) !== FALSE) {
    if (in_array($my_socket, $streams)) {
        while (@socket_recv($my_socket, $data, 8192, 0)) {
            echo $data;
        }
        $lastAccess = time();
    } else {
        if (time()-$lastAccess > LAST_ACCESS_TIMEOUT) {
            break;
        }
    }
    // ...
    $streams = array($my_socket/*, ... */);
}

?>

The solution was simple, but quite hard to find because socket_recv is not documented. socket_recv returns FALSE if there is no data and 0 if the socket is widowed (disconnected by remote side). So I had just to check return value of socket_recv. The problem now sounds stupid, but I've spend some time to find it out.
I hope this will save some of somebody's hair ;)
bastiaan at [no-spam] megabass dot nl
05-Apr-2004 06:35
in case you want to empty/unset $buffer, but failing to do so, try using 0 as flag.
PHP_NORMAL_READ and PHP_BINARY_READ respectively hold 1 and 2 as value.

socket_recvfrom> <socket_read
Last updated: Fri, 26 Sep 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites