- <?php
- echo "Program starts at ". date('h:i:s') . "./n";
- $timeout=10;
- $result=array();
- $sockets=array();
- $convenient_read_block=8192;
- $delay=15;
- $id=0;
- while ( $delay > 0) {
- $s=stream_socket_client("127.0.0.1:80",$errno,$errstr,$timeout,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
- if($s){
- $sockets[$id++]=$s;
- $http_message="GET /test/test1.php?delay=" .$delay . " HTTP/1.0/r/nHost: 127.0.0.1/r/n/r/n";
- fwrite($s, $http_message);
- } else {
- echo "Stream " . $id . " failed to open correctly.";
- }
- $delay -= 3;
- }
- while (count($sockets)) {
- $read=$sockets;
- stream_select($read, $w=null, $e=null, $timeout);
- if(count($read)) {
- foreach($read as $r){
- $id=array_search($r, $sockets);
- $data=fread($r, $convenient_read_block);
- if(strlen($data) == 0) {
- echo "Stream " . $id . " closes at " . date('h:i:s') . "./n";
- fclose($r);
- unset($sockets[$id]);
- } else {
- $result[$id] .= $data;
- }
- }
- } else {
- echo "Time-out!/n";
- break;
- }
- }
php socket 多任务处理
最新推荐文章于 2024-09-13 18:48:07 发布