溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

fsockopen與pfsockopen有什么區(qū)別

發(fā)布時(shí)間:2021-01-20 15:35:08 來(lái)源:億速云 閱讀:138 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了fsockopen與pfsockopen有什么區(qū)別,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

這兩個(gè)函數(shù)的唯一區(qū)別是,pfsockopen是持續(xù)連接,而fsockopen不是.
我寫(xiě)了個(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

上述內(nèi)容就是fsockopen與pfsockopen有什么區(qū)別,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI