|
我寫了個(gè)代碼了一下:
復(fù)制代碼 代碼如下:
<?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;
//發(fā)包函數(shù)
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;
}
?>
結(jié)果發(fā)現(xiàn):
代碼的倒數(shù)第二行,如果把//fclose($conn);注釋掉,結(jié)果是:
fsocket:11.04693198204
pfsocket:0.34867787361145
如果不注釋:
fsocket:12.509312152863
pfsocket:11.120275974274
可以看出,fsocketopen默認(rèn)每次處理結(jié)束后,就算協(xié)議頭是Keep-Alive,連接仍然斷掉了.
而pfsocketopen在Keep-Alive條件下,連接可以被下一次重復(fù)利用.
一次連接發(fā)送大量數(shù)據(jù)時(shí),推薦使用pfsocketopen
php技術(shù):深入解析fsockopen與pfsockopen的區(qū)別,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。