PHP中socket_read的问题

作者:网络 来源:佚名 更新时间:2008-07-07 00:35:37 点击:
,欢迎访问网页设计爱好者web开发。

<?
 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;
  }
 }
*/
?>