Home  >  Article  >  Backend Development  >  PHP中socket_read的问题

PHP中socket_read的问题

WBOY
WBOYOriginal
2016-06-23 14:31:071720browse

简介:这是PHP中socket_read的问题的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=323994' scrolling='no'>


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 "
";
}

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

“PHP中socket_read的问题”的更多相关文章 》

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/323994.html pageNo:15
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP连贯接口Next article:PHP中return的用法