SOCKET_READ(3) 1 SOCKET_READ(3)socket_read - Reads a maximum of length bytes from a socketSYNOPSIS
string socket_read (resource $socket, int $length, [int $type = PHP_BINARY_READ])
DESCRIPTION
The function socket_read(3) reads from the socket resource $socket created by the socket_create(3) or socket_accept(3) functions.
PARAMETERS
o $socket
- A valid socket resource created with socket_create(3) or socket_accept(3).
o $length
- The maximum number of bytes read is specified by the $length parameter. Otherwise you can use
,
, or to end reading
(depending on the $type parameter, see below).
o $type
- Optional $type parameter is a named constant:
o PHP_BINARY_READ (Default) - use the system recv() function. Safe for reading binary data.
o PHP_NORMAL_READ - reading stops at
or
.
RETURN VALUES socket_read(3) returns the data as a string on success, or FALSE on error (including if the remote host has closed the connection). The
error code can be retrieved with socket_last_error(3). This code may be passed to socket_strerror(3) to get a textual representation of the
error.
Note
socket_read(3) returns a zero length string ("") when there is no more data to read.
SEE ALSO socket_accept(3), socket_bind(3), socket_connect(3), socket_listen(3), socket_last_error(3), socket_strerror(3), socket_write(3).
PHP Documentation Group SOCKET_READ(3)
Check Out this Related Man Page
SOCKET_RECV(3) 1 SOCKET_RECV(3)socket_recv - Receives data from a connected socketSYNOPSIS
int socket_recv (resource $socket, string &$buf, int $len, int $flags)
DESCRIPTION
The socket_recv(3) function receives $len bytes of data in $buf from $socket. socket_recv(3) can be used to gather data from connected
sockets. Additionally, one or more flags can be specified to modify the behaviour of the function.
$buf is passed by reference, so it must be specified as a variable in the argument list. Data read from $socket by socket_recv(3) will be
returned in $buf.
PARAMETERS
o $socket
- The $socket must be a socket resource previously created by socket_create().
o $buf
- The data received will be fetched to the variable specified with $buf. If an error occurs, if the connection is reset, or if no
data is available, $buf will be set to NULL.
o $len
- Up to $len bytes will be fetched from remote host.
o $flags
- The value of $flags can be any combination of the following flags, joined with the binary OR ( |) operator.
Possible values for $flags
+-------------+---------------------------------------------------+
| Flag | |
| | |
| | Description |
| | |
+-------------+---------------------------------------------------+
| | |
| MSG_OOB | |
| | |
| | Process out-of-band data. |
| | |
| | |
| MSG_PEEK | |
| | |
| | Receive data from the beginning of the receive |
| | queue without removing it from the queue. |
| | |
| | |
|MSG_WAITALL | |
| | |
| | Block until at least $len are received. However, |
| | if a signal is caught or the remote host discon- |
| | nects, the function may return less data. |
| | |
| | |
|MSG_DONTWAIT | |
| | |
| | With this flag set, the function returns even if |
| | it would normally have blocked. |
| | |
+-------------+---------------------------------------------------+
RETURN VALUES socket_recv(3) returns the number of bytes received, or FALSE if there was an error. The actual error code can be retrieved by calling
socket_last_error(3). This error code may be passed to socket_strerror(3) to get a textual explanation of the error.
EXAMPLES
Example #1
socket_recv(3) example
This example is a simple rewrite of the first example from "Examples" to use socket_recv(3).
<?php
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>
";
/* 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()) . "
";
} else {
echo "OK.
";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.
Reason: ($result) " . socket_strerror(socket_last_error($socket)) . "
";
} else {
echo "OK.
";
}
$in = "HEAD / HTTP/1.1
";
$in .= "Host: www.example.com
";
$in .= "Connection: Close
";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.
";
echo "Reading response:
";
$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)) . "
";
}
socket_close($socket);
echo $buf . "
";
echo "OK.
";
?>
The above example will produce something like:
<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.
PHP Documentation Group SOCKET_RECV(3)