php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64803 async stream_socket_client return
Submitted: 2013-05-09 18:27 UTC Modified: -
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: rdlowrey at gmail dot com Assigned:
Status: Open Package: Streams related
PHP Version: 5.4.15 OS: Fedora 17
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-05-09 18:27 UTC] rdlowrey at gmail dot com
Description:
------------
Normally when creating a client socket connection asynchronously you have to wait 
until the socket stream can be selected as writable to determine whether or not 
the connection succeeded. However, when connecting to a nonexistent server on the 
loopback address there is no way to differentiate between a failed connection and 
a successful connection using the STREAM_CLIENT_ASYNC_CONNECT flag.

The example code connects to a nonexistent server so it should always fail. PHP 
reports the connection as having succeeded and it's not until you actually 
attempt to read or write to the socket stream that you can get an accurate 
`feof()` return value.

Test script:
---------------
<?php
$uri = '127.0.0.1:9000'; // <-- NOTHING LISTENING ON THIS PORT!
$flags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
$socket = stream_socket_client($uri, $errNo, $errStr, $timeout=42, $flags);

usleep(50000); // just in case

var_dump($socket); // resource(5) of type (stream)

$w = [$socket];
$r = $e = [];
if (stream_select($r, $w, $e, 0, 0)) {
    $selectedSock = current($w);
    var_dump($selectedSock); // resource(5) of type (stream) ... says it's writable, BUT IT CAN'T POSSIBLY BE WRITABLE
}

$r = [$socket];
$w = $e = [];
if (stream_select($r, $w, $e, 0, 0)) {
    $selectedSock = current($r);
    var_dump($selectedSock); // resource(5) of type (stream) ... says it's readable, let's check for EOF
}

usleep(50000); // just in case

$isFeof = feof($selectedSock);
var_dump($isFeof); // bool(false) ... not EOF ... WTF?

var_dump(fread($selectedSock, 8192)); // string(0) ""

var_dump(feof($selectedSock)); // bool(TRUE) -- Now we know the truth!

Expected result:
----------------
feof($socket) on an asynchronously connected socket stream should return TRUE if 
the connection attempt failed once the socket becomes "writable."

Actual result:
--------------
It's not possible to determine if the connection attempt failed without first 
attempting to read/write to/from the socket stream.

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Jun 16 21:01:28 2019 UTC