·您的位置: 首页 » 资源教程 » 编程开发 » PHP » PHP中socket_read的问题

PHP中socket_read的问题

类别: PHP教程  评论数:0 总得分:0

<?
 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.1rn";
  $in .= "Host: " . $website . "rn";
  $in .= "Connection: closernrn";
  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.1rn";
   $out .= "Host: " . $website . "rn";
   $out .= "Connection: Closernrn";
   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;
  }
 }
*/
?>

-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1