php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16680 socket_read() does not have the described behaviour ...
Submitted: 2002-04-18 09:57 UTC Modified: 2002-05-05 08:58 UTC
From: tech at kalyweb dot com Assigned:
Status: Closed Package: Sockets related
PHP Version: 4.2.0 OS: Windows 9x
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tech at kalyweb dot com
New email:
PHP Version: OS:

 

 [2002-04-18 09:57 UTC] tech at kalyweb dot com
In the documentation, it's written that socket_read() reads data from the socket until a \n, \t, \0.... or until the end of the buffer.
But under win32 it reads only 1 char.

This would be fixed.

Just use instead :
$buf="";
while (substr($buf,-1)!="\n") {
  $buf.=socket_read($socket,1);
}

I've put 1 here, but you can write 16777216 it'll continue to give back only 1 char.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-04 13:19 UTC] gilles at clixicon dot nl
i have exactly the same problem with win2000.
it closes the connection immidiatly after the first received char.

that little scriptlet upthere seems to be be a way around this.

is this a bug or is it suppost to work like this?
 [2002-05-04 19:07 UTC] mfischer@php.net
Please try with CVS HEAD and report back.
 [2002-05-04 19:37 UTC] mfischer@php.net
How did you test sending data to your php script which received the data? I've noticed that the telnet version shipped with windows does by default transmit every character immidiately over the line (hence resulting only in single characters sent). If I telnet from a linux host to the script running under win32 I recevied the whole data only after I pressed return.

I've tested it with this simple script:
<?
error_reporting(E_ALL);

if (false === ($s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)))
	exit;

if (false === socket_bind($s, '0.0.0.0', 8765))
	exit;

if (false === socket_listen($s, 10))
	exit;

if (false === ($t = socket_accept($s)))
	exit;

while ($data = socket_read($t, 1024)) {
	var_dump($data);
}
?>
 [2002-05-05 08:29 UTC] gilles at clixicon dot nl
i ran your script and connected with teraterm pro from my win2000 box (local):
i typed 'hello world' ans then disconnected.
this is the output i got:

D:\Apache\htdocs>d:\php\php-cli -q -f d:\apache\htdocs\testje.php
string(1) "h"
string(1) "e"
string(1) "l"
string(1) "l"
string(1) "o"
string(1) " "
string(1) "w"
string(1) "o"
string(1) "r"
string(1) "l"
string(1) "d"
 "ring(2) "

i guess this should have been a single line, right?

i couldn't test it at this moment with a connection from my linux box since i'm in a NAT'ed network.
i will try later though.
 [2002-05-05 08:58 UTC] mfischer@php.net
No.

I'm closing this as this is your client which has the habbit to send every single character on its own over the line.

As php.net/socket_select states: "If you read/write to a socket returns in the arrays be aware that they do not necessarily read/write the full amount of data you have requested. Be prepared to even only be able to read/write a single byte."

And, I tried the same thing you did, also with clients from a linux system which only sent the data after I entered return. Just be prepared to received single characters.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Aug 13 15:05:45 2022 UTC