按手册上说,这两个函数的唯一区别是,pfsockopen是持续连接,而fsockopen不是.
我写了个代码了一下:
<?php <BR>$data="1,0,721,73,1,0,0,43290000,0,60D81D509BC00451,3,FFFFFFFF";<BR>//http://10.144.99.114/SANEX_NEW/modules/subscribemanager/test.php<BR>$host = '127.0.0.1';<BR>$url = "/aa.php";<BR>$pffirst = false;<BR>$times = 1000;<BR>$startTime = microtime(true);<BR>for ($index = 0; $index < $times; $index++) {<BR> echo httpPost($host,$url,$data,$pffirst)."<hr><br />";<BR>}<BR>$middleTime = microtime(true);<BR>for ($index = 0; $index < $times; $index++) {<BR> echo httpPost($host,$url,$data,!$pffirst)."<hr><br />";;<BR>}<BR>$endTime = microtime(true);<BR> echo ($pffirst?"pfsocket":"fsocket").":".($middleTime-$startTime);<BR> echo "<br />";<BR> echo ($pffirst?"fsocket":"pfsocket").":".($endTime-$middleTime);<br><br>$count=0;<BR>//发包函数<BR>function httpPost($host,$url,$data,$p)<BR>{<BR>global $count;<BR> $func = $p?"pfsockopen":"fsockopen";<br><br> $conn = $func($host,80,$errno, $errstr, 30);<BR> if (!$conn) <BR> {<BR> echo "$errstr ($errno)<br />\n";<BR> return;<BR> }<br><br> $header = "POST ".$url." HTTP/1.1\r\n";<BR> $header.= "Host : {$host}\r\n";<BR> $header.= "Content-type: application/x-www-form-urlencoded\r\n";<BR> $header.= "Content-Length:".strlen($data)."\r\n";<BR> $header.= "Connection: Keep-Alive\r\n\r\n"; <BR> $header.= "{$data}\r\n\r\n";<br><br> fwrite($conn,$header);<br><br> $count++;<BR> echo $count.' '.$header."<br /><br />";<br><br> $resp='';<BR> //while (!feof($conn)) {<BR> // $resp .= fgets($conn);<BR> //}<BR> //fclose($conn);<BR> return $resp;<BR>}<BR>?><BR>
结果发现:
代码的倒数第二行,如果把//fclose($conn);注释掉,结果是:
fsocket:11.04693198204
pfsocket:0.34867787361145
如果不注释:
fsocket:12.509312152863
pfsocket:11.120275974274
可以看出,fsocketopen默认每次处理结束后,就算协议头是Keep-Alive,连接仍然断掉了
!本文来源gaodai.ma#com搞##代!^码网(
搞gaodaima代码.
而pfsocketopen在Keep-Alive条件下,连接可以被下一次重复利用.
一次连接发送大量数据时,推荐使用pfsocketopen