 |
socket_read (PHP 4 >= 4.1.0, PHP 5) socket_read -- Reads a maximum of length bytes from a socket Descriptionstring socket_read ( resource socket, int length [, int type] )
The function socket_read() reads from the socket
resource socket created by the
socket_create() or
socket_accept() functions. The maximum number of bytes read
is specified by the length parameter. Otherwise
you can use \r, \n, or \0 to end reading
(depending on the type parameter, see below).
socket_read() 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(). This code may be passed to
socket_strerror() to get a textual representation of
the error.
注:
socket_read() returns a zero length string ("")
when there is no more data to read.
Optional type parameter is a named constant:
See also
socket_accept(),
socket_bind(),
socket_connect(),
socket_listen(),
socket_last_error(),
socket_strerror() and
socket_write().
Niels laukens
21-Aug-2006 03:34
This paragraph is confusing:
socket_read() 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(). This code may be passed to socket_strerror() to get a textual representation of the error.
Note: socket_read() returns a zero length string ("") when there is no more data to read.
My tests (on PHP 5.1.4) show that when you socket_read() on a shutdown-socket, it returns FALSE when using PHP_NORMAL_READ, but returns "" when reading in PHP_BINARY_READ.
sbasurto at yahoo dot com
12-Aug-2006 02:28
Interesting use of sockets:
<?php
//Use sockets and xml coool!!!
//Check this out
/*
//============================================================
//First you create a xsl template like the following called test.xsl:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1"/>
<xsl:template match="/">
<table>
<tr><td><font color='red'><xsl:value-of select="test/one"/></font></td></tr>
<tr><td><font color='blue'><xsl:value-of select="test/two"/></font></td></tr>
</table>
</xsl:template>
</xsl:stylesheet>
//============================================================
*/
//============================================================
//Second: You create a php script called getResponse.php with the following content:
echo chr(60).chr(63)."xml version".chr(61)."\"1.0\" encoding".chr(61)."\"ISO".chr(45)."8859".chr(45)."1\" standalone".chr(61)."\"yes\" ".chr(63).chr(62)."\n";
...connect to a database postgres...
$sql = "select first, second from my_table;";
...execute the query and asign to $result array...
$xml_string = "<test>\n";
while($result){
$xml_string .= "<one>".$result[0]['first']."</one>\n";
$xml_string .= "<two>".$result[0]['second']."</two>\n";
}
$xml_string .= "</test>\n";
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Type:text/xml");
echo $xml_string;
//============================================================
//==================S O C K E T S===============================
//Finally: Create a php script called master.php with the following content.
$server = "192.168.0.1"; //my server
$sk = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sk, $server, 80);
$request = "GET /getResponse.php HTTP/1.0"."\r\n";
$request .= "Host:192.168.0.1 \r\n\r\n";
socket_write($sk, $request);
$doc = new DOMDocument();
$xsl = new XSLTProcessor();
$doc->load("./test.xsl");
$xsl->importStyleSheet($doc);
$doc->loadXML(strstr(socket_read($sk,12000),"<?xml"));
echo $xsl->transformToXML($doc);
//==================S O C K E T S================================
"The output is the result of the query one line red and one blue".
//The most interesting thing here, is that you can create all the
//pages of your site with one xsl template and xml, but with
//the advantage of using the power of PHP.
//cool isn't it?
Regards bazz.
?>
Ronin-php at onabout dot net
03-May-2006 03:06
Just a helper for those trying to use sockets to transfer large ammounts of info.
I was pulling my hair out forever trying to figure out why different strings sent by different socket_writes were getting concatenated by socket_read.
If you have a problem with this, try a sleep(), the delay allows the server to see the difference (it is able to do one before the next arrives)
Bill Kuker
18-Mar-2005 11:31
Just a note that on my system the length seems to have an undocumented upper bound of 65536. I was being lazy and not read()ing in a while loop until I pointed it at real data ;)
michi at tr51 dot org
27-May-2004 03:48
if you'd like to make a "socket_read" on a linux-system connected with a flash-client (v. 6.0 r81) you have to send a string to the connected port:
<?php
... //initialising communication
$string = "ready to get/send data\0";
socket_write($socket, $string);
//now you can read from...
$line = trim(socket_read($socket, MAXLINE));
... // do some stuff, finaly close connection
?>
magicking89 at hotmail dot com
31-Aug-2003 07:01
if you want to use a non block socket you must to use socket_last_error
if(!socket_last_error($sc)){
if($buffer=socket_read($sc,512,PHP_NORMAL_READ)){
echo $buffer;
}
}
if you use it your script wont take all your memory
schst at php-tools dot de
05-Jul-2003 09:19
You may download a generic server class at http://www.php-tools.de
This class will accept the sockets read data from it and hands it to a callback function. Furthermore there are methods for connection handling included.
25-Sep-2002 02:48
Windows telnet sends/recieves one character at a time. Try adding PHP_NORMAL_READ to the end of socket_read, that might help.
|  |