Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4998721
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类:

2010-09-25 17:41:12

<?php
$url = "";
if(array_key_exists('url',$_GET)){
$url = $_GET['url'];
}
$hosts = array("", "", "","","","vip.xunlei.com");
#$hosts = array($url);

$timeout = 5;
$status = array();
$retdata = array();
$sockets = array();
$e = array();
/* Initiate connections to all the hosts simultaneously */
foreach ($hosts as $id => $host) {
$errno = 0;
$errstr = "";
$s = stream_socket_client("$host:80", $errno, $errstr, $timeout,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
if ($s) {
   $sockets[$id] = $s;
   $status[$id] = "in progress";
} else {
   $status[$id] = "failed, $errno $errstr";
}
$retdata[$id] = '';
}
/* Now, wait for the results to come back in */
while (count($sockets)) {
$read = $write = $sockets;
    /* This is the magic function - explained below */
$n = stream_select($read, $write, $e, $timeout);
if ($n > 0) {
     /* readable sockets either have data for us, or are failed connection attempts */
   foreach ($read as $r) {
    $id = array_search($r, $sockets);
    $data = fread($r, 8192);
    if (strlen($data) == 0) {
     if ($status[$id] == "in progress") {
      $status[$id] = "failed to connect";
     }
     fclose($r);
     unset($sockets[$id]);
    } else {
     $retdata[$id] .= $data;
    }
   }
   /* writeable sockets can accept an HTTP request */
   foreach ($write as $w) {
    if(!is_resource($w))continue;
    $id = array_search($w, $sockets);
    fwrite($w, "GET / HTTP/1.0\r\nHost: ".$hosts[$id]."\r\n\r\n");
    $status[$id] = "waiting for response";
   }
} else {
     /* timed out waiting; assume that all hosts associated
     * with $sockets are faulty */

   foreach ($sockets as $id => $s) {
    $status[$id] = "timed out " . $status[$id];
   }
   break;
}
}
foreach ($hosts as $id => $host) {
#echo "Host: $host\n";

#echo "Status: " . $status[$id] . "\n";

#echo "Retdata: " . $retdata[$id] . "\n";

$strs = explode("\r\n\r\n",$retdata[$id],2);
echo isset($strs[1])?$strs[1]:$retdata[$id];
}
function debug($i){
var_dump($i);
var_dump(gettype($i));
var_dump(is_resource($i));
}
?>


阅读(2546) | 评论(0) | 转发(0) |
0

上一篇:php fsockopen 与 stream_select

下一篇:QueryCache

给主人留下些什么吧!~~