php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52571 socket_read function returns max 1305 bytes remote, OK local
Submitted: 2010-08-09 22:45 UTC Modified: 2010-08-12 08:27 UTC
From: jim_ivers at americantours dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.3.3 OS: Centos 5 (redhat clone)
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
32 - 27 = ?
Subscribe to this entry?

 
 [2010-08-09 22:45 UTC] jim_ivers at americantours dot com
Description:
------------
I have a php script running on apache that reads a string from a TCP socket on a server. Using the below script on a local server works. I cannot get it to return more than 1305 bytes remotely. The remote server reports sending the whole string (about 4K bytes) back. 

The following is a short version of the code below:
   while(($sting = socket_read($socket,  $slen, PHP_BINARY_READ)) !== false) {
        $out = $out.$sting;
        if(strlen($out) >= $slen) break;
   }

I get a warning that the socket closed, then the string:
<b>Warning</b>:  socket_read() [<a href='function.socket-read'>function.socket-read</a>]: unable to read from socket [104]: Connection reset by peer in <b>/usr/local/apache2/htdocs/mikidel.php</b> on line <b>473</b><br />
"returnstringabcdefg....."

using php 5.3.3 w Apache/2.2.14 (Unix) PHP/5.3.3 on Centos 5 ( redhat clone)

./configure --enable-xml --enable-dom --with-openssl --enable-sockets --enable-debug --with-config-file-path=/etc  --with-curl --with-zlib --enable-ftp  --enable-soap --with-sybase-ct=/sybase --with-libxml-dir=/usr  --prefix=/usr/local/apache2/php --with-config-file-path=/usr/local/apache/php --with-gettext --with-apxs2=/usr/local/apache2/bin/apxs

php.ini standard vanilla

note that the remote socket is C code, and I have tried using write(socket...) and send(socket...). Both work locally since there are no \n or \r. Neither work remotely.



Test script:
---------------
   // Create a TCP/IP Socket
   $socket = socket_create (AF_INET, SOCK_STREAM, 0);
   if ($socket < 0) { echo "socket() failed: reason: " . strerror ($socket) . "\n"; }

   // Connect to Target
   $result = socket_connect ($socket, $address, $service_port);
   if ($result < 0) { echo "connect() failed.\nReason: ($result) " . strerror($result) . "\n"; }

   $tmp = socket_read ($socket, 6, PHP_BINARY_READ);
   $slen = (int)($tmp-6);
   $out = (string)$tmp;

$sting = socket_read($socket, $slen, PHP_BINARY_READ);
$out = $out.$sting;

   while(($sting = socket_read($socket,  $slen, PHP_BINARY_READ)) !== false) {
        $out = $out.$sting;
        if(strlen($out) >= $slen) break;
   }

   // Close Socket
   socket_close ($socket);

   return $out."\n";

Expected result:
----------------
The script should work remotely since it works locally.

Obviously if I ask for 4k bytes from a remote socket, it should return 4k bytes, since it does so from a local socket.

Actual result:
--------------
<b>Warning</b>:  socket_read() [<a href='function.socket-read'>function.socket-read</a>]: unable to read from socket [104]: Connection reset by peer in <b>/usr/local/apache2/htdocs/mikidel.php</b> on line <b>473</b><br />
"returnstringabcdefg....."

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-11 20:29 UTC] jim_ivers at americantours dot com
-Status: Open +Status: Closed
 [2010-08-11 20:29 UTC] jim_ivers at americantours dot com
To clarify, the socket server is remote or local, the php code is always local.

Turns out one of the libraries on the remote server is using unicode, 4 bytes to the character. Thus the string it is trying to return is 4 times as long as the string length.
 [2010-08-12 08:27 UTC] mike@php.net
-Status: Closed +Status: Bogus
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 09 23:01:27 2024 UTC