PHP.nl

socket_recv

socket_recv

Receives data from a connected socket

 **socket_recv** Socket $socket  $data int $length int $flags

The function receives bytes of data in from . can be used to gather data from connected sockets. Additionally, one or more flags can be specified to modify the behaviour of the function. socket_recv``length``data``socket``socket_recv

is passed by reference, so it must be

specified as a variable in the argument list. Data read from by will be returned in . data``socket``socket_recv``data

socket The must be a instance previously created by . socket``Socket``socket_create

data The data received will be fetched to the variable specified with . If an error occurs, if the connection is reset, or if no data is available, will be set to null. data``data

length Up to bytes will be fetched from remote host. length

flags The value of can be any combination of the following flags, joined with the binary OR () operator. flags``|

returns the number of bytes received,

or false if there was an error. The actual error code can be retrieved by calling . This error code may be passed to to get a textual explanation of the error. socket_recv``socket_last_error``socket_strerror

Voorbeeld: example

 This example is a simple rewrite of the first example from
  to
 use .
`socket_recv`
<?php
error_reporting(E_ALL);

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

/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');

/* Get the IP address for the target host. */
$address = gethostbyname('www.example.com');

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
    echo "OK.\n";
}

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
    echo "socket_connect() failed.\nReason: ($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 "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";

echo "Reading response:\n\n";
$buf = 'This is my buffer.';
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
    echo "Read $bytes bytes from socket_recv(). Closing socket...";
} else {
    echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
socket_close($socket);

echo $buf . "\n";
echo "OK.\n\n";
?>
<h2>TCP/IP Connection</h2>
OK.
Attempting to connect to '208.77.188.166' on port '80'...OK.
Sending HTTP HEAD request...OK.
Reading response:

Read 123 bytes from socket_recv(). Closing 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.