|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2023-05-08 16:59 UTC] bukka@php.net
-Status: Open
+Status: Duplicate
-Type: Bug
+Type: Feature/Change Request
-Assigned To:
+Assigned To: bukka
[2023-05-08 16:59 UTC] bukka@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 20:00:01 2025 UTC |
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.