#21197 [Com]: socket_read() outputs error with PHP_NORMAL_READ

From: Date: Wed, 03 Aug 2005 12:22:34 +0000
Subject: #21197 [Com]: socket_read() outputs error with PHP_NORMAL_READ
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-82873@lists.php.net to get a copy of this message
 ID:               21197
 Comment by:       susenjit_c at hotmail dot com
 Reported By:      bool at boolsite dot net
 Status:           Open
 Bug Type:         Sockets related
 Operating System: win32 only
 PHP Version:      5.*, 4.*
 New Comment:

Hello all,
socket_read() is working fine omitting the third parameter
PHP_NORMAL_READ. 

I am using php 5.0.4 in WinXP with Apache 2.0.54. Server code is same
as example given in online document with minor modification. Using CLI
the code works fine. I have written a small client and run it from
browser which sends text message to the server. This also working fine.


The only problem I am having now is that I can't run the server code
using browser cause its hanging and using telnet client I can only send
single character. For this reason in my server code I have taken '9' for
Quit and '0' for Shutdown.

Part of the code goes as -
<?php

/* Same as example code in the document and then */
    	$msg = "\nWelcome to the PHP Test Server. \n";
        $msg .= "To quit, type '9'. To shut down the server type
'0'.\n";
    	socket_write($msgsock, $msg, strlen($msg)); 
    	do {
    		if (false === ($buf = socket_read($msgsock, 2048))) {   /*,
PHP_NORMAL_READ*/
    			echo "socket_read() failed: reason: " . socket_strerror($ret) .
"\n";
    			break 2;
    		}
    		if ($buf == '9') {
    			$msg1 = "\nLoging out. \n Thanks for using the server";
    			socket_write($msgsock, $msg1, strlen($msg));
    			break;
    		}
    		if ($buf == '0') {
    			$msg1 = "\nServer is shuting down. Thanks.";
    			socket_write($msgsock, $msg1, strlen($msg));
    			socket_close($msgsock);
    			break 2;
    		}
    		$talkback = "\tHOST : You said " .$buf. "\n";
    		socket_write($msgsock, $talkback, strlen($talkback));
    		echo "$buf\n";
    	}  while (true);
    	echo "Closing client.\n";
    	socket_close($msgsock);
}  while (true);
socket_close($sock);
?>

Susen


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

[2005-07-23 19:34:13] temposeb at free dot fr

Configuration : PHP 5.1.0b3 (CLI), Win2000 SP4.

I make my first steps in network dev / sockets and I encounter the same
problem.
A simpler complete test case using PHP-CLI instead of the unreadable
sample given sooner.
I want simply connect the client to the "server" and send the msg
"Hello server !" that will be displayed on the server's console...

server.php
=========================================================
<?php
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) ;
    socket_bind($socket, '127.0.0.1', 4567) ;
    socket_listen($socket) ;

    $client = socket_accept($socket) ;
    echo socket_read($client, 256, PHP_NORMAL_READ) ; // Line 7
    socket_close($client) ;

    socket_close($socket) ;
?>
=========================================================

client.php
=========================================================
<?php
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) ;
    socket_connect($socket, '127.0.0.1', 4567) ;

    socket_write($socket, "Hello server !\r\n") ;

    socket_close($socket) ;
?>
=========================================================

So, I launch server.php then client.php, result on server console :

=========================================================
PHP Warning:  socket_read(): unable to read from socket [0]: Opération
réussie.
 in C:\dev\PHP\mines\server.php on line 7

Warning: socket_read(): unable to read from socket [0]: Opération
réussie.
 in C:\dev\PHP\mines\server.php on line 7
=========================================================

Same thing with PHP_BINARY_READ or no 3rd parameter specified :
socket_read( ) returns FALSE and warn us.
No error on client console.

Note that all *seems* OK when I use the following server.php :
=========================================================
<?php
    $socket = stream_socket_server('tcp://127.0.0.1:4567') ;

    $client = stream_socket_accept($socket) ;
    echo fgets($client) ; // Outputs "Hello server !"
    fclose($client) ;

    fclose($socket) ;
?>
=========================================================

So, how should we consider socket extension ? Should we forget it for
stream_*( ) and f*( ) functions ?
Thx :)

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

[2004-03-11 11:06:02] nlopess@php.net

I've compilled PHP with cygwin/gcc and no error is produced. However,
the build version from snaps.php.net gives that error.

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

[2003-08-26 02:00:58] bool at boolsite dot net

Ok, this is a short example : (a little echo server)

<?php
error_reporting(E_ALL);

$Port=6669;

if(($Sock=socket_create(AF_INET,SOCK_STREAM,0))<=0) {
	echo 'socket_create() a échoué :
',socket_strerror(socket_last_error($Sock)),"\r\n";
	exit;
	}
if(($Ret=socket_bind($Sock,0,$Port))<=0) {
	echo 'socket_bind() a échoué :
',socket_strerror(socket_last_error($Ret)),"\r\n";
	exit;
	}
if(($Ret=socket_listen($Sock,5))<=0) {
	echo 'socket_listen() a échoué :
',socket_strerror(socket_last_error($Ret)),"\r\n";
	exit;
	}

while(true){
	$MsgSock=socket_accept($Sock);
	if($MsgSock===false) {
        echo 'socket_accept() a échoué :
',socket_strerror(socket_last_error($MsgSock)),"\r\n";
        break;
        }
      else {
	    echo '=> Debut de la connexion...',"\r\n";
		$EndTime=time()+15;
		do{
            $buffer=socket_read($MsgSock,1024,PHP_NORMAL_READ);
            if($buffer===false) {
			    echo 'socket_read() a échoué :
',socket_strerror(socket_last_error($MsgSock)),"\r\n";
				break;
                }
            elseif(!$buffer){
				continue;
                }
            $buffer=trim($buffer);
			echo '< ',$buffer,"\r\n";
			if($buffer=='quit') {
				break;
                }

            $back='You sent : ['.$buffer.']';

			echo '> ',$back,"\r\n";
			socket_write($MsgSock,$back."\r\n");
            } while(time()<$EndTime);

        @socket_close($MsgSock);
        echo '=> End...',"\r\n";
        }
    }
socket_close($Sock);
?>

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

[2002-12-26 20:39:22] edink@php.net

If you omit the third parameter to socket_read() it seems to work fine.
However, adding PHP_NORMAL_READ causes error as described in the bug
report.

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

[2002-12-26 09:32:25] bool at boolsite dot net

Hello

I have a source which works with PHP 4.1.x to PHP 4.2.x,
it's work perfectly. But with PHP 4.3RC4 (windows version,
client mode) I have this warning :
Warning: socket_read() unable to read from socket [0]: OpÚration
rÚussie. in E:\PHP\KioobFTP\v0.7.1\KioobFTP_SocketMode.php on line 262

Then, the result of the function is FALSE. 
The socket is in blocking mode.
The code is :
$tmp=socket_read($this->stream,4096,PHP_NORMAL_READ);

Do you need others info ?

Thanks.

Bool

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


-- 
Edit this bug report at http://bugs.php.net/?id=21197&edit=1


Thread (46 messages)

« previous php.bugs (#82873) next »