#35086 [NEW]: socket_read does not return empty string after receiving '\n'

From: Date: Thu, 03 Nov 2005 13:41:09 +0000
Subject: #35086 [NEW]: socket_read does not return empty string after receiving '\n'
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-87467@lists.php.net to get a copy of this message
From:             jelte dot werkhoven at cruisetravel dot nl
Operating system: GNU/Linux (SuSE 10.0)
PHP version:      4.4.1
PHP Bug Type:     Sockets related
Bug description:  socket_read does not return empty string after receiving '\n'

Description:
------------
This seems to be a replica of bug #21880, but I can replicate it each
time. I've got a proxy daemon written in C that terminates data with a
line break (linux '\n'). As I understand it, this should cause socket_read
to return an empty string, if called with the PHP_NORMAL_READ flag.
However, it doesn't and just hangs. BTW, version is PHP 4.4.0 CLI.
Connecting to said proxy daemon with either netcat or telnet works as
expected.

Reproduce code:
---------------
/* Get the port for the WWW service. */
$service_port = 8001;

/* Get the IP address for the target host. */
$address = gethostbyname('localhost');

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
   echo "socket_create() failed: reason: " . socket_strerror($socket) .
"\n";
} else {
   echo "OK got socket.\n";
}

echo "Attempting to connect to '$address' on port '$service_port'...";

$result = @socket_connect($socket, $address, $service_port);

if (!$result) {
   die("socket_connect() failed.\nReason: " .
socket_strerror(socket_last_error()) . "\n");
} else {
   echo "OK got connection.\n";
}

$in = "REQ\n";

$out = '';

socket_write($socket, $in, strlen($in));
echo "OK wrote connection string '$in'.\n";

echo "Reading daemon response:\n";
while( $out = socket_read($socket, 2048, PHP_NORMAL_READ) ) {
	//if( strncmp( $out, 'ACK', 3 ) == 0 ) break;	
	if( $out === false ) echo "FALSE!";
	if( $out == '' ) echo "ZILCH!";
}



Expected result:
----------------
Commented out strncmp line is workaround, without it, it never breaks out
of the while loop which I think it should. I figure it should echo either
ZILCH on receiving '\n' which is prepended to the ACK reply of the daemon,
or FALSE as the daemon disconnects after sending the ACK string.
If I use it with the workaround I can actually see that the line
terminator is sent because PHP echo's it when I echo the $out string.



Actual result:
--------------
OK got socket.
Attempting to connect to '127.0.0.1' on port '8001'...OK got connection.
OK wrote connection string 'REQ
'.
Reading daemon response:


And it hangs indefinately.

-- 
Edit bug report at http://bugs.php.net/?id=35086&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=35086&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=35086&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=35086&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=35086&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=35086&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=35086&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=35086&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=35086&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=35086&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=35086&r=notwrong
Not enough info:             http://bugs.php.net/fix.php?id=35086&r=notenoughinfo
Submitted twice:             http://bugs.php.net/fix.php?id=35086&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=35086&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=35086&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=35086&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=35086&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=35086&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=35086&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=35086&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=35086&r=mysqlcfg


Thread (10 messages)

« previous php.bugs (#87467) next »