php.net |  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
Votes:1
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
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”

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-13 21:55 UTC] pajoye@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: mattficken
 [2011-10-13 21:55 UTC] pajoye@php.net
Matt, please analyze that one.
 [2011-10-14 23:37 UTC] mattficken@php.net
-Status: Assigned +Status: Bogus
 [2011-10-14 23:37 UTC] mattficken@php.net
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