Analyze the differences between fsockopen and pfsockopen

  • 2020-07-21 07:05:49
  • OfStack

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

<?php 
$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)."<hr><br />";
}
$middleTime = microtime(true);
for ($index = 0; $index < $times; $index++) {
 echo httpPost($host,$url,$data,!$pffirst)."<hr><br />";;
}
$endTime = microtime(true);
 echo ($pffirst?"pfsocket":"fsocket").":".($middleTime-$startTime);
 echo "<br />";
 echo ($pffirst?"fsocket":"pfsocket").":".($endTime-$middleTime);

$count=0;
// Contract awarding 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)<br />\n";
  return;
 }

 $header = "POST ".$url." HTTP/1.1\r\n";
 $header.= "Host : {$host}\r\n";
 $header.= "Content-type: application/x-www-form-urlencoded\r\n";
 $header.= "Content-Length:".strlen($data)."\r\n";
 $header.= "Connection: Keep-Alive\r\n\r\n"; 
 $header.= "{$data}\r\n\r\n";

 fwrite($conn,$header);

 $count++;
 echo $count.' '.$header."<br /><br />";

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

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

If not annotated:
fsocket:12.509312152863
pfsocket:11.120275974274
As you can see,fsocketopen defaults to the end of each processing, even if the protocol header is Keep-ES22en, the connection still fails.
While pfsocketopen in Keep-ES26en condition, the connection can be reused in the next time.
pfsocketopen is recommended when a single connection sends a large amount of data

Related articles: