深入解析fsockopen與pfsockopen的區(qū)別
更新時(shí)間:2013年07月05日 11:57:10 作者:
本篇文章是對(duì)fsockopen與pfsockopen的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
按手冊(cè)上說,這兩個(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實(shí)現(xiàn)將Session寫入數(shù)據(jù)庫(kù)
這篇文章主要介紹了php實(shí)現(xiàn)將Session寫入數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2015-07-07PHP7擴(kuò)展開發(fā)之基于函數(shù)方式使用lib庫(kù)的方法詳解
這篇文章主要介紹了PHP7擴(kuò)展開發(fā)之基于函數(shù)方式使用lib庫(kù)的方法,結(jié)合實(shí)例形式分析了PHP7中l(wèi)ib庫(kù)擴(kuò)展的封裝與調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2018-01-01php流量統(tǒng)計(jì)功能的實(shí)現(xiàn)代碼
用php實(shí)現(xiàn)的流量統(tǒng)計(jì)功能代碼,本代碼僅供學(xué)習(xí)交流,其中必有不妥之處。請(qǐng)見諒2012-09-09php中使用array_filter()函數(shù)過濾數(shù)組實(shí)例講解
在本篇文章里小編給大家分享的是一篇關(guān)于php中使用array_filter()函數(shù)過濾數(shù)組實(shí)例講解,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03基于PHP實(shí)現(xiàn)短信驗(yàn)證碼發(fā)送次數(shù)限制
這篇文章主要介紹了基于PHP實(shí)現(xiàn)短信驗(yàn)證碼發(fā)送次數(shù)限制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07php設(shè)計(jì)模式之裝飾模式應(yīng)用案例詳解
這篇文章主要介紹了php設(shè)計(jì)模式之裝飾模式,結(jié)合具體應(yīng)用案例形式詳細(xì)分析了php裝飾模式的概念、原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-06-06php代碼中使用換行及(\n或\r\n和br)的應(yīng)用
瀏覽器識(shí)別不了\n或\r\n,這兩個(gè)換行符是文本換行符,文本文件有效;如果需要將結(jié)果輸出到瀏覽器或打印到顯示器,代碼中使用br;如果只是在源代碼中換行,則使用\n或\r\n,感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)php有所幫助2013-02-02PHP實(shí)現(xiàn)Socket服務(wù)器的代碼
2008-04-04