深入解析fsockopen与pfsockopen的区别
发布时间:2020-12-13 06:30:18 所属栏目:PHP教程 来源:网络整理
导读:按手册上说,这两个函数的唯一区别是, pfsockopen 是持续连接,而 fsockopen 不是. 我写了个代码了一下: div class="codetitle" a style="CURSOR: pointer" data="85712" class="copybut" id="copybut85712" onclick="doCopy('code85712')" 代码如下: div clas
按手册上说,这两个函数的唯一区别是, pfsockopen是持续连接,而fsockopen不是.我写了个代码了一下: <div class="codetitle"><a style="CURSOR: pointer" data="85712" class="copybut" id="copybut85712" onclick="doCopy('code85712')"> 代码如下:<div class="codebody" id="code85712"><?php $data="1,721,73,1,43290000,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)." "; } $middleTime = microtime(true); for ($index = 0; $index < $times; $index++) { echo httpPost($host,!$pffirst)." ";; } $endTime = microtime(true); echo ($pffirst?"pfsocket":"fsocket").":".($middleTime-$startTime); echo " "; echo ($pffirst?"fsocket":"pfsocket").":".($endTime-$middleTime);$count=0; //发包函数 function httpPost($host,$p) { global $count; $func = $p?"pfsockopen":"fsockopen";$conn = $func($host,80,$errno,$errstr,30); if (!$conn) { echo "$errstr ($errno) n"; return; }$header = "POST ".$url." HTTP/1.1rn"; $header.= "Host : {$host}rn"; $header.= "Content-type: application/x-www-form-urlencodedrn"; $header.= "Content-Length:".strlen($data)."rn"; $header.= "Connection: Keep-Alivernrn"; $header.= "{$data}rnrn";fwrite($conn,$header);$count++; echo $count.' '.$header." ";$resp=''; //while (!feof($conn)) { //$resp .= fgets($conn); //} //fclose($conn); return $resp; } ?> <FONT style="COLOR: #ff0000">结果发现: 代码的倒数第二行,如果把//fclose($conn);注释掉,结果是: fsocket:11.04693198204 pfsocket:0.34867787361145 <FONT style="COLOR: #ff0000">fsocket:12.509312152863 |