|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55814 stream_socket_recvfrom() “randomly” returns false
Submitted: 2011-09-29 14:23 UTC Modified: 2011-10-15 06:33 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: ivan dot enderlin at hoa-project dot net Assigned: mattficken (profile)
Status: Not a bug Package: Streams related
PHP Version: Irrelevant OS: Windows 7
Private report: No CVE-ID: None
 [2011-09-29 14:23 UTC] ivan dot enderlin at hoa-project dot net
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 = stream_socket_client(
echo 'Received ';
var_dump(stream_socket_recvfrom($client, 6));


$server = stream_socket_server(
echo 'Up & listen…', "\n";
$client = stream_socket_accept($server);
echo 'New connection', "\n";
stream_socket_sendto($client, 'foobar');
echo 'Sent “foobar”', "\n";

Expected result:
Received: string(6) "foobar"

Up & listen…
New connection
Sent “foobar”

Actual result:
Sometimes: Received: string(6) "foobar"
Sometimes: Received: bool(false)

Up & listen…
New connection
Sent “foobar”


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-13 21:55 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: mattficken
 [2011-10-13 21:55 UTC]
Matt, please analyze that one.
 [2011-10-14 23:37 UTC]
-Status: Assigned +Status: Bogus
 [2011-10-14 23:37 UTC]
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-15 06:33 UTC] 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-17 08:51 UTC] ivan dot enderlin at hoa-project dot net
Matt, I would like to know if the stream_get_line() and stream_get_contents() functions are also asynchronous? Thanks.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 13 13:01:30 2024 UTC