|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2014-04-02 12:34 UTC] mike@php.net
-Status: Open
+Status: Feedback
[2014-04-02 12:34 UTC] mike@php.net
[2014-12-30 10:41 UTC] php-bugs at lists dot php dot net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 04:00:01 2025 UTC |
Description: ------------ In order to communicate with many servers at the same time in a persistent way, i use the stream_socket_client function. Once the connection is established with all the servers, I wait for the http timeout of each connection. The servers send a "close_wait". When i try to reused the previous connections, a new connection is automaticaly establish with the first server while all other connections are reused with a feof available. Why the first connection not have the same comportment than other ? Test script: --------------- <!DOCTYPE html> <meta charset="utf-8"> <title>Bug in stream_socket_client ?</title> <pre><?php ob_start(); $remotes = array ( 'cl108.advertstream.com' => '91.121.95.33', 'cl109.advertstream.com' => '94.23.35.164', 'cl111.advertstream.com' => '94.23.206.224', 'cl121.advertstream.com' => '91.121.163.5', ); $errno; $errstr; $read = []; foreach ($remotes as $key => $target) { do { $socket = stream_socket_client( 'tcp://'.$target.':80', $errno, $errstr, 0.700, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT ); stream_set_blocking($socket, 0); $reused = (bool) ftell($socket); fread($socket, 4096); $want_close = feof($socket); $local = stream_socket_get_name($socket, false); if (true === $want_close) { echo $key . ' (' . $local . '): want_close' . PHP_EOL; fclose($socket); continue; } if (true === $reused) { echo $key . ' (' . $local . '): reused connection' . PHP_EOL; } else { echo $key . ' (' . $local . '): new connection' . PHP_EOL; } echo PHP_EOL; break; } while (true); $read[] = $socket; $message = "GET / HTTP/1.1\r\n". "Host: $host\r\n". "Connection: Keep-Alive\r\n". "\r\n"; fwrite($socket, $message); } $write; $except; $originalRead = $read; $success = 0; $iteration = count($originalRead); do { stream_select($read, $write, $except, 0, 500e3); foreach ($read as $stream) { $bytes = fread($stream, 4096); if (false !== strpos($bytes, "0\r\n\r\n")) { $success++; } } if ($iteration === $success) { break; } $read = $originalRead; } while (true); ob_end_flush(); ?></pre> Expected result: ---------------- - calling script (1) cl108.advertstream.com (192.168.2.10:59816): new connection cl109.advertstream.com (192.168.2.10:52000): new connection cl111.advertstream.com (192.168.2.10:43161): new connection cl121.advertstream.com (192.168.2.10:39815): new connection - watching netstat tcp 0 0 michel-p6-2312ef.:59816 cl108.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:52000 cl109.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:43161 cl111.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:39815 cl121.advertstream:http ESTABLISHED - waiting for http timeout... - watching netstat tcp 1 0 michel-p6-2312ef.:59816 cl108.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:52000 cl109.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:43161 cl111.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:39815 cl121.advertstream:http CLOSE_WAIT - calling script (2) cl108.advertstream.com (192.168.2.10:59816): want_close <<<< EXPECTED <<<< cl108.advertstream.com (192.168.2.10:59821): new connection cl109.advertstream.com (192.168.2.10:52000): want_close cl109.advertstream.com (192.168.2.10:52005): new connection cl111.advertstream.com (192.168.2.10:43161): want_close cl111.advertstream.com (192.168.2.10:43166): new connection cl121.advertstream.com (192.168.2.10:39815): want_close cl121.advertstream.com (192.168.2.10:39820): new connection - watching netstat tcp 0 0 michel-p6-2312ef.:59821 cl108.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:52005 cl109.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:43166 cl111.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:39820 cl121.advertstream:http ESTABLISHED - waiting for http timeout... - watching netstat tcp 1 0 michel-p6-2312ef.:59821 cl108.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:52005 cl109.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:43166 cl111.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:39820 cl121.advertstream:http CLOSE_WAIT - calling script (3) cl108.advertstream.com (192.168.2.10:59821): want_close <<<< EXPECTED <<<< cl108.advertstream.com (192.168.2.10:59826): new connection cl109.advertstream.com (192.168.2.10:52005): want_close cl109.advertstream.com (192.168.2.10:52010): new connection cl111.advertstream.com (192.168.2.10:43166): want_close cl111.advertstream.com (192.168.2.10:43171): new connection cl121.advertstream.com (192.168.2.10:39820): want_close cl121.advertstream.com (192.168.2.10:39825): new connection tcp 0 0 michel-p6-2312ef.:59826 cl108.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:52010 cl109.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:43171 cl111.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:39825 cl121.advertstream:http ESTABLISHED - waiting for http timeout... - watching netstat tcp 1 0 michel-p6-2312ef.:59826 cl108.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:52010 cl109.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:43171 cl111.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:39825 cl121.advertstream:http CLOSE_WAIT - ... Actual result: -------------- - calling script (1) cl108.advertstream.com (192.168.2.10:59816): new connection cl109.advertstream.com (192.168.2.10:52000): new connection cl111.advertstream.com (192.168.2.10:43161): new connection cl121.advertstream.com (192.168.2.10:39815): new connection - watching netstat tcp 0 0 michel-p6-2312ef.:59816 cl108.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:52000 cl109.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:43161 cl111.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:39815 cl121.advertstream:http ESTABLISHED - waiting for http timeout... - watching netstat tcp 1 0 michel-p6-2312ef.:59816 cl108.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:52000 cl109.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:43161 cl111.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:39815 cl121.advertstream:http CLOSE_WAIT - calling script (2) cl108.advertstream.com (192.168.2.10:59821): new connection cl109.advertstream.com (192.168.2.10:52000): want_close cl109.advertstream.com (192.168.2.10:52005): new connection cl111.advertstream.com (192.168.2.10:43161): want_close cl111.advertstream.com (192.168.2.10:43166): new connection cl121.advertstream.com (192.168.2.10:39815): want_close cl121.advertstream.com (192.168.2.10:39820): new connection - watching netstat tcp 0 0 michel-p6-2312ef.:59821 cl108.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:52005 cl109.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:43166 cl111.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:39820 cl121.advertstream:http ESTABLISHED - waiting for http timeout... - watching netstat tcp 1 0 michel-p6-2312ef.:59821 cl108.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:52005 cl109.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:43166 cl111.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:39820 cl121.advertstream:http CLOSE_WAIT - calling script (3) cl108.advertstream.com (192.168.2.10:59826): new connection cl109.advertstream.com (192.168.2.10:52005): want_close cl109.advertstream.com (192.168.2.10:52010): new connection cl111.advertstream.com (192.168.2.10:43166): want_close cl111.advertstream.com (192.168.2.10:43171): new connection cl121.advertstream.com (192.168.2.10:39820): want_close cl121.advertstream.com (192.168.2.10:39825): new connection tcp 0 0 michel-p6-2312ef.:59826 cl108.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:52010 cl109.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:43171 cl111.advertstream:http ESTABLISHED tcp 0 0 michel-p6-2312ef.:39825 cl121.advertstream:http ESTABLISHED - waiting for http timeout... - watching netstat tcp 1 0 michel-p6-2312ef.:59826 cl108.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:52010 cl109.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:43171 cl111.advertstream:http CLOSE_WAIT tcp 1 0 michel-p6-2312ef.:39825 cl121.advertstream:http CLOSE_WAIT - ...