Edit report at https://bugs.php.net/bug.php?id=55814&edit=1
ID: 55814
Comment by: ivan dot enderlin at hoa-project dot net
Reported by: ivan dot enderlin at hoa-project dot net
Summary: stream_socket_recvfrom() ârandomlyâ returns
false
Status: Bogus
Type: Bug
Package: Streams related
Operating System: Windows 7
PHP Version: Irrelevant
Assigned To: mattficken
Block user comment: N
Private report: N
New Comment:
Matt, I would like to know if the stream_get_line() and stream_get_contents()
functions are also asynchronous? Thanks.
Previous Comments:
------------------------------------------------------------------------
[2011-10-15 06:33:47] ivan dot enderlin at hoa-project dot net
Hi Matt,
I understand the âproblemâ and I understand perfectly what you're proposing
to me. But I don't understand why stream_socket_recvfrom() is not blocking
here. Reading a socket is always blocking, isn't it?
I will change the behavior for Windows7 only and coming back with feedbacks.
Thanks for your time.
------------------------------------------------------------------------
[2011-10-14 23:37:55] [email protected]
I can reproduce this behavior on Windows 7, but not Linux (both using php
5.3.8).
The manual entry for stream_socket_recvfrom, isn't clear on this, but really it
just reads whatever message may be in the stream buffer (if any). Typically its
used with stream_select.
This is asynchronous IO, so you can't assume the message will be there when you
call stream_socket_recvfrom.
Even on Linux, if you're going to a remote host (your example is a localhost),
it may not work sometimes due to network traffic, number of connections, etc...
Windows network scheduling has different behavior than Linux. Thats why this
'problem' occurred. Also, because of those differences, if you call usleep(1)
to sleep for just a 1 ms before calling stream_socket_recvfrom, the message
will get into the buffer (because you're putting your program to sleep so
Windows can use that time to do other work).
Also, if you do: stream_set_blocking($client, true); before
stream_socket_recvfrom, you'll convert the $client stream to synchronous IO
(whereas its asynchronous by default) and your example will work then.
I recommend you do that, or rewrite your code to use synchronous functions like
fread().
------------------------------------------------------------------------
[2011-10-13 21:55:46] [email protected]
Matt, please analyze that one.
------------------------------------------------------------------------
[2011-09-29 14:23:07] ivan dot enderlin at hoa-project dot net
Description:
------------
Sometimes, on Windows7 with PHP5.3+, stream_socket_recvfrom() returns false
(the documentation says that it always returns a string but the source code
says the opposite, it can return false), and I have no idea why. It happens
only on Windows7 a priori. Basically, I have a client and a server as bellow.
Test script:
---------------
Client.php
<?php
$client = stream_socket_client(
'tcp://127.0.0.1:9001',
$errno,
$errstr,
30,
STREAM_CLIENT_CONNECT
);
echo 'Received ';
var_dump(stream_socket_recvfrom($client, 6));
Server.php
<?php
$server = stream_socket_server(
'tcp://127.0.0.1:9001',
$errno,
$errstr,
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN
);
echo 'Up & listenâ¦', "\n";
$client = stream_socket_accept($server);
echo 'New connection', "\n";
stream_socket_sendto($client, 'foobar');
echo 'Sent âfoobarâ', "\n";
Expected result:
----------------
Client.php
Received: string(6) "foobar"
Server.php
Up & listenâ¦
New connection
Sent âfoobarâ
Actual result:
--------------
Client.php
Sometimes: Received: string(6) "foobar"
Sometimes: Received: bool(false)
Server.php
Up & listenâ¦
New connection
Sent âfoobarâ
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55814&edit=1