深入解析fsockopen與pfsockopen的區(qū)別
更新時(shí)間:2013年07月05日 11:57:10 作者:
本篇文章是對(duì)fsockopen與pfsockopen的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
按手冊(cè)上說(shuō),這兩個(gè)函數(shù)的唯一區(qū)別是,pfsockopen是持續(xù)連接,而fsockopen不是.
我寫了個(gè)代碼了一下:
<?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
我寫了個(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
相關(guān)文章
php+js實(shí)現(xiàn)百度地圖多點(diǎn)標(biāo)注的方法
這篇文章主要介紹了php+js實(shí)現(xiàn)百度地圖多點(diǎn)標(biāo)注的方法,涉及php結(jié)合js針對(duì)百度地圖接口調(diào)用與json操作相關(guān)技巧,需要的朋友可以參考下2016-11-11
PHP轉(zhuǎn)換文本框內(nèi)容為HTML格式的方法
這篇文章主要介紹了PHP轉(zhuǎn)換文本框內(nèi)容為HTML格式的方法,通過(guò)自定義函數(shù)實(shí)現(xiàn)字符串轉(zhuǎn)換為HTML格式的功能,涉及php針對(duì)HTML標(biāo)簽的替換技巧,需要的朋友可以參考下2016-07-07
關(guān)于在php.ini中添加extension=php_mysqli.dll指令的說(shuō)明
關(guān)于在php.ini中添加extension=php_mysqli.dll指令的說(shuō)明...2007-06-06
PHP通過(guò)調(diào)用新浪API生成t.cn格式短網(wǎng)址鏈接的方法詳解
這篇文章主要介紹了PHP通過(guò)調(diào)用新浪API生成t.cn格式短網(wǎng)址鏈接的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了php調(diào)用新浪API生成t.cn格式短網(wǎng)址鏈接的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-02-02
自定義session存儲(chǔ)機(jī)制避免會(huì)話保持問(wèn)題
PHP服務(wù)端session以文件的方式存儲(chǔ),當(dāng)用戶訪問(wèn)量過(guò)大時(shí)就會(huì)面臨會(huì)話保持的問(wèn)題,下面有兩種解決方案,需要的朋友可以參考下2014-10-10
PHP新手用的Insert和Update語(yǔ)句構(gòu)造類
PHP新手用的Insert和Update語(yǔ)句構(gòu)造類,沒(méi)多大功能,學(xué)習(xí)php的朋友可以參考下2012-03-03

