SunshinePHP Developer Conference 2015

socket_recv

(PHP 4 >= 4.1.0, PHP 5)

socket_recvEmpfängt Daten von einem verbundenen Socket

Beschreibung

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

Die socket_recv() Funktion empfängt len Bytes Daten in buf von socket. socket_recv() kann genutzt werden, um Daten von verbundenen Sockets zu lesen. Zusätzlich können einer oder mehrere Flags angegeben werden, um das Verhalten der Funktion zu modifizieren.

buf wird als Referenz übergeben, daher muss er als Variable in der Liste der Argumente angegeben werden. Gelesene Daten von socket durch socket_recv() werden in buf zurückgegeben.

Parameter-Liste

socket

Der socket muss eine socket Ressource sein, die zuvor mit socket_create() erzeugt wurde.

buf

Empfangene Daten werden in die Variable geschrieben, die durch buf spezifiziert ist. Tritt ein Fehler auf, wird die Verbindung zurückgesetzt oder, wenn keine Daten verfügbar sind, wird buf auf NULL gesetzt.

len

Maximal len Bytes werden von dem entfernten System abgerufen.

flags

Der Wert von flags kann jede beliebige Kombination der folgenden Flags sein, verknüpft mit dem binären ODER (|) Operator.

Mögliche Werte von flags
Flag Beschreibung
MSG_OOB Verarbeite bandexterne (out-of-band) Daten.
MSG_PEEK Lies Daten ab Anfang der Empfangs-Warteschlange, ohne die Daten aus der Warteschlange zu löschen.
MSG_WAITALL Blockiere so lange, bis mindestens len Daten gelesen wurden. Wenn jedoch ein Signal empfangen wurde oder der entfernte Host die Verbindung beendet, kann die Funktion auch weniger Daten zurückgeben.
MSG_DONTWAIT Wenn dieses Flag gesetzt ist, kehrt die Funktion zurück, auch wenn sie normalerweise blockieren würde.

Rückgabewerte

socket_recvfrom() gibt die Anzahl der empfangenen Bytes zurück oder FALSE, falls ein Fehler auftrat. Der aktuelle Fehlercode kann mit socket_last_error() abgefragt werden. Dieser Fehlercode kann an die Funktion socket_strerror() übergeben werden, um eine textuelle Beschreibung des Fehlers zu erhalten.

Beispiele

Beispiel #1 socket_recv() Beispiel

Dies ist eine einfache Umformulierung des ersten Beispieles von Beispiele unter Nutzung von socket_recv().

<?php
error_reporting
(E_ALL);

echo 
"<h2>TCP/IP Verbindung</h2>\n";

/* Erhalte Port für den WWW Service. */
$service_port getservbyname('www''tcp');

/* Erhalte die IP Adresse des Ziel Hosts. */
$address gethostbyname('www.example.com');

/* Erzeuge ein TCP/IP Socket. */
$socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
if (
$socket === false) {
    echo 
"socket_create() schlug fehl: Grund: " socket_strerror(socket_last_error()) . "\n";
} else {
    echo 
"OK.\n";
}

echo 
"Versuche Verbing zu '$address' auf Port '$service_port' aufzubauen...";
$result socket_connect($socket$address$service_port);
if (
$result === false) {
    echo 
"socket_connect() schulg fehl.\nGrund: ($result) " socket_strerror(socket_last_error($socket)) . "\n";
} else {
    echo 
"OK.\n";
}

$in "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out '';

echo 
"Sende HTTP HEAD Request...";
socket_write($socket$instrlen($in));
echo 
"OK.\n";

echo 
"Lese Response:\n\n";
$buf 'Dies ist mein Puffer.';
if (
false !== ($bytes socket_recv($socket$buf2048MSG_WAITALL))) {
    echo 
"Las $bytes bytes von socket_recv(). Schliesse Socket...";
} else {
    echo 
"socket_recv() schlug fehl; Grund: " socket_strerror(socket_last_error($socket)) . "\n";
}
socket_close($socket);

echo 
$buf "\n";
echo 
"OK.\n\n";
?>

Obiges Beispiel erzeugt eine Ausgabe ähnlich wie:

<h2>TCP/IP Verbindung</h2>
OK.
Versuche Verbing zu '208.77.188.166' auf Port '80' aufzubauen...OK.
Sende HTTP HEAD Request...OK.
Lese Response:

Schliesse Socket...HTTP/1.1 200 OK
Date: Mon, 14 Sep 2009 08:56:36 GMT
Server: Apache/2.2.3 (Red Hat)
Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
ETag: "b80f4-1b6-80bfd280"
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8

OK.

add a note add a note

User Contributed Notes 10 notes

up
9
dgk at tcde dot ru
9 years ago
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 ;)
up
3
ss-130 at yandex dot ru
1 year ago
<?php
$er
= error_reporting(0);
$bytes    = socket_recv($socket,$buffer,1,MSG_WAITALL);
error_reporting($er);

// MEGA BUG HERE
// this statuses are wrong and swapped, closed socket must be with "FALSE"
// but in fact he swap the values:
// http://php.net/manual/en/function.socket-recv.php
//
if($bytes===false){ // no data available, socket not closed
   
echo 'WS_READ_ERR1: '.socket_strerror(socket_last_error($socket)).PHP_EOL;
   
// print when no data available:
    // WS_READ_ERR1: Resource temporarily unavailable
   
continue;
}else if(
$bytes===0){ // socket closed
   
echo 'WS_READ_ERR2: '.socket_strerror(socket_last_error($socket)).PHP_EOL;
   
// print when socket closed:
    // WS_READ_ERR2: Success
   
$process->close();
}

?>
up
1
rathamahata at rathamahata dot net
9 years ago
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/
up
2
m_lajos at hotmail dot com
3 months ago
Workaround for the missing MSG_DONTWAIT flag according to the bug report page:

<?php if(!defined('MSG_DONTWAIT')) define('MSG_DONTWAIT', 0x40); ?>
up
2
davide dot renzi at gmail dot com
2 years ago
In PHP version 5.* there is a bug: MSG_DONTWAIT flag is not defined (see https://bugs.php.net/bug.php?id=48326)
up
1
Anonymous
9 years ago
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.
up
1
bastiaan at [no-spam] megabass dot nl
10 years ago
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.
up
0
cottton at i-stats dot net
7 days ago
socket_recv()
returns FALSE if client returned no data
returns 0 (zero) if client disconnected

also (asuming case socket_select() "gave" us a "changed" socket):
if
socket_recv() returned FALSE
and no bytes were received
then
client "crashed" (call it disconnected).

else if
socket_recv() returned 0 (zero)
and no bytes were received
then
client "normaly" disconnected.

Im pretty sure -- 99.99%.
Example:
<?php
function receive($socket)
{
   
// !
    // on all following cases we assume that
    // socket_select() returned the current socket as "changed"
    // !

   
$timeout = 3; // set your timeout

    /* important */
   
$socket_recv_return_values['no_data_received'] = false;
   
$socket_recv_return_values['client_disconnected'] = 0;

   
$start = time();
   
$received_data = null;
   
$received_bytes = null;
   
socket_set_nonblock($socket);
   
socket_clear_error();
    while(
        (
$t_out=((time()-$start) >= $timeout)) === false
       
and ($read=@socket_recv($socket, $buf, 4096, 0)) >= 1
   
){
       
$received_data  = (isset($received_data)) ? $received_data . $buf : $buf;
       
$received_bytes = (isset($received_bytes)) ? $received_bytes + $read : $read;
    }
   
$last_error = socket_last_error($socket);
   
socket_set_block($socket);

    if(
$t_out === true){
        throw new
Exception(
           
'timeout after ' . ((!$received_bytes) ? 0 : $received_bytes) . ' bytes',
           
0 // your eCode here
       
);
    }
    elseif(
$last_error !== false and $last_error !== 0){
        throw new
Exception(
           
socket_strerror($last_error),
           
$last_error
       
);
    }
    else{
        if(
$read === $socket_recv_return_values['no_data_received']){
           
// client returned NO DATA
            // but we were in a loop and could have got some data before:
           
if($received_bytes < 1){
               
// client is connected but sent NO DATA ?
                // no:
                // in this case the client must be "crashed" because -
                // it is not possible to "send no data" (zero bytes)
                // socket_select() now returns this socket as "changed" "forever"
               
throw new Exception(
                   
'client crashed',
                   
0 // your eCode here
               
);
            }else{
               
// client returned DATA
               
return $received_data;
            }
        }
        elseif(
$read === $socket_recv_return_values['client_disconnected']){
           
// client disconnected
           
if($received_bytes < 1){
               
// client disconnected before/without sending any bytes
               
throw new Exception(
                   
'client disconnected',
                   
0 // your eCode here
               
);
            }
            else{
               
// *this value* ^= $socket_recv_return_values['client_disconnected']
                //
                // client disconnected AFTER sending data (we were in a loop!)
                // socket_select() will return this socket "forever" as "changed" and -
                // socket_recv() will return *this value* "forever".
                // we will be "back" again "very soon" to see:
                //  socket_recv() returns *this value* AND no bytes received
                //  which results in disconnect-exception above
               
return $received_data;
            }
        }
    }
}
?>
up
0
Anonymous
9 years ago
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.
up
0
engine at [NO SPAM] illusiononly dot com
9 years ago
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;
        }

     }
To Top