#28141 [Opn]: socket_read return type: false vs "", when PHP_NORMAL_READ
| From: | sniper@php.net | Date: | Mon, 14 Feb 2005 21:23:02 +0000 |
| Subject: | #28141 [Opn]: socket_read return type: false vs "", when PHP_NORMAL_READ | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969366591@lists.php.net to get a copy of this message | ||
ID: 28141
Updated by: sniper@php.net
Reported By: php at richardneill dot org
Status: Open
Bug Type: Documentation problem
Operating System: Linux
PHP Version: 4CVS, 5CVS (2005-02-14)
Assigned To: andrey
New Comment:
Forgot this part from that: recv() function is what socket_read() uses
internally.
Previous Comments:
------------------------------------------------------------------------
[2005-02-14 22:20:51] sniper@php.net
From recv() manpage:
"Upon successful completion, recv() shall return the length of the
message in bytes. If no messages are available to be received and the
peer has performed an orderly shutdown, recv() shall return 0.
Otherwise, -1 shall be returned and errno set to indicate the error."
------------------------------------------------------------------------
[2005-02-14 17:30:00] php at richardneill dot org
Thanks. That makes sense. However, it still leaves a few problems,
essentially with wrong documentation.
-------------------
1)The documentation for socket_read
http://uk.php.net/manual/en/function.socket-read.php
claims:
"Note: socket_read() may return a zero length string ("") indicating
the end of communication (i.e. the remote end point has closed the
connection)."
which is obviously not true.
---------------------
2)Example 1 on the sockets page
http://uk.php.net/manual/en/ref.sockets.php
also supports the interpretation that
False => error; null => connection closed
if (false === ($buf = socket_read($msgsock, 2048,
PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " .
socket_strerror($ret) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
----------------------
3)How is one to determine the difference between
i)the other side closing the connection normally
and
ii)something going very wrong with the function?
According to the documentation,
"" => (i)
false => (ii)
In fact, it seems that
false => (i) or (ii), with no way to tell
"" => never actually happens
Thanks for your help,
Richard
------------------------------------------------------------------------
[2005-02-14 09:10:12] sniper@php.net
There is no bug to fix here. You're actually getting "Connection reset
by peer" (ECONNRESET) and that makes recv() to return -1 -> PHP
socket_read() returns an error and FALSE.
Here's simplified WORKING script:
#!/usr/local/bin/php
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting as it
comes in. */
ob_implicit_flush();
$address = '127.0.0.1';
$port = 1111;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, $address, $port);
socket_listen($sock, 5);
do {
$msgsock = socket_accept($sock);
do {
$buf = socket_read($msgsock, 2048, PHP_NORMAL_READ);
var_dump($buf);
} while ($buf !== FALSE && $buf !== '');
echo socket_strerror(socket_last_error($msgsock));
socket_close($msgsock);
echo "Closed msgsocket; waiting for another connection\n";
} while (true);
socket_close($sock);
echo "Closed main socket, exiting\n";
?>
------------------------------------------------------------------------
[2004-04-25 06:56:24] php at richardneill dot org
Description:
------------
According to the documentation, socket_read() can return:
1)A string - normal data
2)An empty string "" - the other end closed the connection
3)false - something went wrong.
But it seems to be returning false on connection close.
Reproduce code:
---------------
$buffer=socket_read($socket,2048,PHP_NORMAL_READ);
if ($buffer===false){
echo "Error: socket_read() failed: reason:
".socket_strerror(socket_last_error())." \n";
exit (1);
}elseif ($buffer==''){
echo "Socket $socket returned an empty string. Closing
connection\n";
socket_close($socket);
}else{
echo "Received data".trim($buffer)."\n";
}
Expected result:
----------------
I'm using netcat as a client, and then killing the netcat process with
Ctrl-C to simulate remote host disconnecting.
I expect to see the socket close.
Actual result:
--------------
Actually, the php script exits, because socket_read returns
false instead of "".
This may be a bug in the documentation for socket_read, rather than in
its behaviour.
Thanks for your help - Richard
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=28141&edit=1