ID:               28141
 Updated by:       [EMAIL PROTECTED]
 Reported By:      php at richardneill dot org
-Status:           Open
+Status:           Feedback
 Bug Type:         Sockets related
 Operating System: Linux
 PHP Version:      5.0.0RC1
 New Comment:

I cannot reproduce the problem with latest PHP5. Can you provide a
*full* reproducing case.
My scripts are :
server:
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '127.0.0.1';
$port = 4322;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
   echo "socket_create() failed: reason: " . socket_strerror($sock) .
"\n";
}
if (($ret = socket_bind($sock, $address, $port)) < 0) {
   echo "socket_bind() failed: reason: " . socket_strerror($ret) .
"\n";
}
if (($ret = socket_listen($sock, 5)) < 0) {
   echo "socket_listen() failed: reason: " . socket_strerror($ret) .
"\n";
}
do {
   if (($msgsock = socket_accept($sock)) < 0) {
       echo "socket_accept() failed: reason: " .
socket_strerror($msgsock) . "\n";
       break;
   }

   $buffer=socket_read($msgsock,2048,PHP_NORMAL_READ);
   $buffer2=socket_read($msgsock,2048,PHP_NORMAL_READ);

   var_dump($buffer, $buffer2);
   if ($buffer===false){
        echo "Error: socket_read() failed: reason:
".socket_strerror(socket_last_error())." \n";
        break;
   } else if ($buffer=='') {
        echo "Socket $socket returned an empty string. Closing
connection\n";
        socket_close($socket);
   } else {
        echo "Received data".trim($buffer)."\n";
   }

   socket_close($msgsock);
} while (true);
socket_close($sock);
?>
client:
<?php
error_reporting(E_ALL);
echo "TCP/IP Connection\n";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
   echo "socket_create() failed: reason:
".socket_strerror($socket)."\n";
} else {
   echo "OK.\n";
}
echo "Attempting to connect to '127.0.0.1' on port '4322'...";
$result = socket_connect($socket, "127.0.0.1", 4322);
if ($result < 0) {
   echo "socket_connect() failed.\nReason: ($result)
".socket_strerror($result) . "\n";

} else {
   echo "OK.\n";
}
$s = "ALA\n";
socket_write($socket, $s);
echo "Closing socket...";sleep(5);
socket_close($socket);
echo "OK.\n\n";
?>



Previous Comments:
------------------------------------------------------------------------

[2004-04-28 04:54:43] php at richardneill dot org

This is still present in RC2

------------------------------------------------------------------------

[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

Reply via email to