深入解析fsockopen与pfsockopen的区别_PHP教程
程序员文章站
2022-05-11 22:26:31
...
按手册上说,这两个函数的唯一区别是,pfsockopen是持续连接,而fsockopen不是.
我写了个代码了一下:
$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 echo httpPost($host,$url,$data,$pffirst)."
";
}
$middleTime = microtime(true);
for ($index = 0; $index echo httpPost($host,$url,$data,!$pffirst)."
";;
}
$endTime = microtime(true);
echo ($pffirst?"pfsocket":"fsocket").":".($middleTime-$startTime);
echo "
";
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)
\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."
";
$resp='';
//while (!feof($conn)) {
// $resp .= fgets($conn);
//}
//fclose($conn);
return $resp;
}
?>
结果发现:
代码的倒数第二行,如果把//fclose($conn);注释掉,结果是:
fsocket:11.04693198204
pfsocket:0.34867787361145
如果不注释:
fsocket:12.509312152863
pfsocket:11.120275974274
可以看出,fsocketopen默认每次处理结束后,就算协议头是Keep-Alive,连接仍然断掉了.
而pfsocketopen在Keep-Alive条件下,连接可以被下一次重复利用.
一次连接发送大量数据时,推荐使用pfsocketopen
我写了个代码了一下:
复制代码 代码如下:
$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 echo httpPost($host,$url,$data,$pffirst)."
";
}
$middleTime = microtime(true);
for ($index = 0; $index echo httpPost($host,$url,$data,!$pffirst)."
";;
}
$endTime = microtime(true);
echo ($pffirst?"pfsocket":"fsocket").":".($middleTime-$startTime);
echo "
";
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)
\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."
";
$resp='';
//while (!feof($conn)) {
// $resp .= fgets($conn);
//}
//fclose($conn);
return $resp;
}
?>
结果发现:
代码的倒数第二行,如果把//fclose($conn);注释掉,结果是:
fsocket:11.04693198204
pfsocket:0.34867787361145
如果不注释:
fsocket:12.509312152863
pfsocket:11.120275974274
可以看出,fsocketopen默认每次处理结束后,就算协议头是Keep-Alive,连接仍然断掉了.
而pfsocketopen在Keep-Alive条件下,连接可以被下一次重复利用.
一次连接发送大量数据时,推荐使用pfsocketopen
上一篇: OpenCart框架运行流程介绍
下一篇: 用C实现PHP扩展类的步骤
推荐阅读
-
php连接函数implode与分割explode的深入解析
-
Android中asset文件夹与raw文件夹的区别深入解析
-
解析php中array_merge与array+array的区别
-
php include和require的区别深入解析
-
解析php函数method_exists()与is_callable()的区别
-
PHP中mb_convert_encoding与iconv函数的深入解析
-
深入分析php中接口与抽象类的区别
-
php中用加号与用array_merge合并数组的区别深入分析
-
解析php addslashes()与add*es()函数的区别和比较
-
深入解析PHP中逗号与点号的区别