PHP中Socket_Read的问题_PHP教程

WBOY
發布: 2016-07-20 11:00:01
原創
945 人瀏覽過

 

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

for ($i=0; $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;
}
}
*/
?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445541.htmlTechArticleerror_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($web...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!