socket_read的问题!!!!!---引用

<?
 error_reporting(E_ALL);
 set_time_limit(0);
 $runtimes = 100;

 for ($i=0; $i<$runtimes; $i++)
 {
  $str = f_socket("www.baidu.com","/s?wd=10000" . $i);
  echo "<textarea>" . $str . "</textarea><br>";
 }

 function f_socket($website,$url)
 {
  $service_port = getservbyname('www', 'tcp');
  $address = gethostbyname($website);

  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  if (false == ($socket_result = socket_connect($socket, $address, $service_port)))
  {
   echo ("SOCKET_CONNECT_ERROR: " . socket_strerror(socket_last_error($socket)));
  }

  $in = "GET " . $url . " HTTP/1.1/r/n";
  $in .= "Host: " . $website . "/r/n";
  $in .= "Connection: close/r/n/r/n";
  socket_write($socket, $in, strlen($in));

  $start_time = time();
  $str = "";
  do
  {
   if (false === ($out = socket_read($socket, 8192)))
   {
    echo ("SOCKET_READ_ERROR: " . socket_strerror(socket_last_error($socket)));
    $str = "";
    break;
   }
   if (time() - $start_time > 1)
   {
    echo ("SOCKET_READ_ERROR: Timeout!!!");
    $str = "";
    break;
   }
   $str .= $out;
  } while ($out != "");

  socket_close($socket);
  return $str;
 }
/*
 function f_socket($website,$url)
 {
  $fp = fsockopen($website, 80, $errno, $errstr, 30);
  if (!$fp)
  {
  }
  else
  {
   $out = "GET " . $url . " HTTP/1.1/r/n";
   $out .= "Host: " . $website . "/r/n";
   $out .= "Connection: Close/r/n/r/n";
   fwrite($fp, $out);
   $str = "";
   $start_time = time();
   while (!feof($fp))
   {
    $str .= fread($fp, 8192);
    if (time()-$start_time > 1)
    {
     $str = "";
     echo "timeout!!!";
       break;
    }
   }
   return $str;
  }
 }
*/
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值