程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP中Socket_Read的問題

PHP中Socket_Read的問題

編輯:關於PHP編程

 

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 "

" . $str . "

";
}

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


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved