php模拟socket一次连接,多次发送数据的实现代码

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?php
//post.php
function Post($host,$port)
{
//$host="127.0.0.1";
//建立连接
$conn = fsockopen($host,$port);
if (!$conn)
{
die("Con error");
}
//循环发送5次数据
//
for($i = 0;$i<5;$i++)
{
$data="user_name=admin".$i;
WriteData($conn,$host,$data);
echo $i."<br />";
}
fclose($conn);
}
function WriteData($conn,$host,$data)
{
$header = "POST /test.php 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";
//Keep-Alive是关键
$header.= "Connection: Keep-Alive\r\n\r\n";
$header.= "{$data}\r\n\r\n";
fwrite($conn,$header);
//取结果
//$result = '';
//while(!feof($conn))
//{
// $result .= fgets($conn,128);
//}
//return $result;
}
Post('127.0.0.1',80);
?>

复制代码 代码如下:

<?php
//test.php
$fp = fopen('result.txt','a');
$data = $_POST['user_name']." -- ". date('Y-m-d H:i:s')."\r\n";
fwrite($fp,$data);
fclose($fp);
?>

相关文章

php中读写文件与读写数据库的效率比较分享

这个问题也是最近才想到的,就是到底读文件更快还是读数据库更快,能快多少,天缘也搜索过,没见有网友就这个问题答复过,也可能是太简单的缘故,我们本文还是来实测一下,由于时间关系,VC还没装,...

解析PHP中常见的mongodb查询操作

复制代码 代码如下:<?php// 欄位字串為$querys = array("name"=>"shian");// 數值等於多少$querys = array("numbe...

PHP数组遍历的几种常见方式总结

本文实例讲述了PHP数组遍历的几种常见方式。分享给大家供大家参考,具体如下: 1、使用for循环遍历数组 conut($arr);用于统计数组元素的个数。 for循环只能用于遍历,纯索引...

PHP7正式版测试,性能惊艳!

PHP7正式版测试,性能惊艳!

我们今天就来看一下PHP 7正式版的算法和 wordpress 应用在其上的性能表现。 PHP7 的安装,真是非常地向下兼容,下载,解压,把之前的配置命令用上,一路回车下去,毫无违和感。...

使用PHP会话(Session)实现用户登陆功能

对比起 Cookie,Session 是存储在服务器端的会话,相对安全,并且不像 Cookie 那样有存储长度限制,本文简单介绍 Session 的使用。由于 Session 是以文本文...