In-depth analysis of the difference between fsockopen and pfsockopen_PHP tutorial

WBOY
Release: 2016-07-21 15:00:22
Original
836 people have browsed it

According to the manual, the only difference between these two functions is that pfsockopen is a persistent connection, while fsockopen is not.
I wrote a code:

Copy code The code is as follows:

$data="1,0,721,73 ,1,0,0,43290000,0,60D81D509BC00451,3,FFFFFFFF";
//http://10.144.99.114/SANEX_NEW/modules/subscribemanager/test.php
$host = '127.0.0.1 ';
$url = "/aa.php";
$pffirst = false;
$times = 1000;
$startTime = microtime(true);
for ($index = 0; $index < $times; $index++) {
echo httpPost($host,$url,$data,$pffirst)."

";
}
$middleTime = microtime(true);
for ($index = 0; $index < $times; $index++) {
echo httpPost($host,$url,$data,!$pffirst) ."

";;
}
$endTime = microtime(true);
echo ($pffirst?"pfsocket":"fsocket").":" .($middleTime-$startTime);
echo "
";
echo ($pffirst?"fsocket":"pfsocket").":".($endTime-$middleTime) ;

$count=0;
//Post function
function httpPost($host,$url,$data,$p)
{
global $count;
$func = $p?"pfsockopen":"fsockopen";

$conn = $func($host,80,$errno, $errstr, 30);
if (!$conn)
{
echo "$errstr ($errno)
n";
return;
}

$header = "POST ".$url." HTTP/1.1rn";
$header.= "Host : {$host}rn";
$header.= "Content-type: application/x-www-form-urlencodedrn";
$ header.= "Content-Length:".strlen($data)."rn";
$header.= "Connection: Keep-Alivernrn";
$header.= "{$data}rnrn";

fwrite($conn,$header);

$count++;
echo $count.' '.$header."

";

$resp='';
//while (!feof($conn)) {
// $resp .= fgets($conn);
//}
//fclose($conn);
return $resp;
}
?>

The result is:
The penultimate line of code, if //fclose($conn); is commented out, the result is:
fsocket:11.04693198204
pfsocket:0.34867787361145

If not commented:
fsocket:12.509312152863
pfsocket:11.120275974274
It can be seen that fsocketopen defaults to after each processing, even if the protocol header is Keep-Alive, the connection is still disconnected Lost.
With pfsocketopen under the Keep-Alive condition, the connection can be reused next time.
When sending a large amount of data in one connection, it is recommended to use pfsocketopen

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328070.htmlTechArticleAccording to the manual, the only difference between these two functions is that pfsockopen is a persistent connection, while fsockopen is not. I I wrote a code: Copy the code as follows: ?php $data="1,0,721,73,1,...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!