深入解析fsockopen与pfsockopen的区别

编程学习 2021-07-05 09:49www.dzhlxh.cn编程入门
本篇文章是对fsockopen与pfsockopen的区别进行了详细的分析介绍,需要的朋友参考下
按手册上说,这两个函数的唯一区别是,pfsockopen是持续连接,而fsockopen不是.
我写了个代码了一下:
代码如下:

<?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;
//发包函数
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);
 /

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板