Deep analysis of the difference between Fsockopen and Pfsockopen

Source: Internet
Author: User

According to the manual, the only difference between the two functions is that thePfsockopen is a continuous connection, while the Fsockopen is not.
I wrote a code for a moment:

Copy Code code as follows:


<?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 function


function HttpPost ($host, $url, $data, $p)


{


Global $count;


$func = $p? " Pfsockopen ":" Fsockopen ";





$conn = $func ($host, $errno, $errstr, 30);


if (! $conn)


 {


echo "$errstr ($errno) <br/>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. " <br/><br/> ";





$resp = ';


//while (!feof ($conn)) {


//$resp. = Fgets ($conn);


 //}


//fclose ($conn);


return $RESP;


}


?>


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

If you do not comment:
fsocket:12.509312152863
pfsocket:11.120275974274
Can be seen, fsocketopen default each processing end, even if the protocol head is keep-alive, the connection is still broken off.
and Pfsocketopen under keep-alive conditions, the connection can be reused for the next time.
Pfsocketopen is recommended when sending large amounts of data on a single connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.