php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49759 socket_read() fails to return empty string
Submitted: 2009-10-03 12:42 UTC Modified: 2009-10-04 18:16 UTC
From: chmod3 at googlemail dot com Assigned:
Status: Not a bug Package: Sockets related
PHP Version: 5.3.0 OS: WinVista & FreeBSD
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: chmod3 at googlemail dot com
New email:
PHP Version: OS:

 

 [2009-10-03 12:42 UTC] chmod3 at googlemail dot com
Description:
------------
According to documentation:

"Note: socket_read() returns a zero length string ("") when there is no more data to read."

This does not seem to be the case on my installations:
PHP 5.3.0 WinVista
PHP 5.2.11 WinVista
PHP 5.2.11 FreeBSD
PHP 5.2.10 WinVista
PHP 5.2.9 WinVista

socket_read() just hangs.

Reproduce code:
---------------
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket, 'news.giganews.com', 119);

$response = '';
do {
  $recv = '';
  echo "START READ\n";
  $recv = socket_read($socket, 512);
  echo "END READ\n";
  var_dump($recv);
  if($recv != '') {
    $response .= $recv;
  }
} while($recv != '');

echo 'OUTPUT: ' . $reply;

Expected result:
----------------
START READ
END READ
string(23) "200 News.GigaNews.Com
"
START READ
END READ
OUTPUT: 200 News.GigaNews.Com

Actual result:
--------------
START READ
END READ
string(23) "200 News.GigaNews.Com
"
START READ

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-03 13:46 UTC] fa@php.net
I think your example code makes no sense.

When I change it a bit, it works as expected (Did you check PHP_BINARY READ vs. PHP_NORMAL_READ?)

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket, 'news.giganews.com', 119);

do {
  $recv = '';
  echo "//START READ\n";
  $recv = socket_read($socket, 1 );
  var_dump($recv);
  echo "\\\\END READ\n\n";

} while($recv != '' && $recv !== false && $recv != "\n");

echo 'OUTPUT';
?>


I do agree that the docs could be clarified.
 [2009-10-03 14:14 UTC] chmod3 at googlemail dot com
Yes your code works only if PHP_NORMAL_READ is added. Adding PHP_BINARY_READ or ommiting either will make it hang. I was hoping to use binary so my script will handle the /r/n

CODE
----

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = socket_connect($socket, 'news.giganews.com', 119);

do {
  echo "-- START READ\n";
  $recv = socket_read($socket, 512, PHP_NORMAL_READ);
  var_dump($recv);
  echo "-- END READ\n";
} while ($recv !== '' && $recv !== FALSE && $recv !== "\n");
 [2009-10-04 09:42 UTC] chmod3 at googlemail dot com
So is socket_read() supposed to hang by design when in PHP_BINARY_READ mode when there is no more data to read?
 [2009-10-04 18:09 UTC] Sjoerd@php.net
socket_read() intentionally blocks until there is some data.
 [2009-10-04 18:16 UTC] chmod3 at googlemail dot com
Understood, so it's a documentation 'bug'.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Jan 29 11:03:35 2022 UTC